+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
Coding Thread, Visual Basic - Changing a command with manual text input in Coding and Web Development; Sure this will be possible, I've had a search on the web, but as it's a bit difficult to explain ...
  1. #1
    gtg93's Avatar
    Join Date
    May 2010
    Posts
    316
    Thank Post
    183
    Thanked 41 Times in 33 Posts
    Rep Power
    15

    Visual Basic - Changing a command with manual text input

    Sure this will be possible, I've had a search on the web, but as it's a bit difficult to explain I'm struggling to find an answer:

    I'm attempting to make a small tool (to use on multiple sites), that basically automates the jobs we'd usually run batch files and vb scrips for at the click of a button.

    Currently, I'm looking at adding the printers.

    form.png

    code.png

    Basically, when the "Add" button is clicked in the first image, the code from "Set printers" in the second image is run, but I want "SERVERNAME" and "PRINTERNAME" to be replaced by what is typed by the user in the retrospective text fields above.

    Is this possible, and if so how? I image its just a "ValueOfTextbox1\ValueOfTextbox2" type thing?

    Thanks in advance for any help

    edit - excuse the disgraceful design, this is just thrown together to show what I mean.
    Last edited by gtg93; 25th June 2014 at 02:12 PM.

  2. #2
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    593
    Thank Post
    99
    Thanked 135 Times in 119 Posts
    Rep Power
    49
    try...

    <variable> = me.<nameoftextbox>.text

  3. #3
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    593
    Thank Post
    99
    Thanked 135 Times in 119 Posts
    Rep Power
    49
    Sorry to elaborate:

    In this case I would do the following...

    Code:
    Dim SERVERNAME as string = me.textbox1.text
    Dim PRINTERNAME as string = me.textbox2.text
    
    ....
    
    objNetwork.AddWindowsPrinterConnection("\\" & SERVERNAME & "\" & PRINTERNAME & ")
    HTH

  4. Thanks to sister_annex from:

    gtg93 (25th June 2014)

  5. #4
    gtg93's Avatar
    Join Date
    May 2010
    Posts
    316
    Thank Post
    183
    Thanked 41 Times in 33 Posts
    Rep Power
    15
    Thanks! - I should have thought of that, I've done that recently but still quite new to it...

    error.png
    I'm now getting this error, but can't seem to find an issue with my code. Nothing is showing in the "Error List" - I've read the linked articles but can't seem to relate the issues to my project.

    latest code.png
    Any ideas?

  6. #5
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    593
    Thank Post
    99
    Thanked 135 Times in 119 Posts
    Rep Power
    49
    Move the DIMs in to the button click event code

  7. Thanks to sister_annex from:

    gtg93 (25th June 2014)

  8. #6

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,139
    Thank Post
    860
    Thanked 2,692 Times in 2,282 Posts
    Blog Entries
    9
    Rep Power
    771
    Quote Originally Posted by sister_annex View Post
    Move the DIMs in to the button click event code
    Beat me to it, at the moment it is populating those variables with nothing as it runs when the form is created and the user has not yet entered anything in so the value is null. By putting it in the onClick handler it only runs when the user has (hopefully) added the required information.

  9. Thanks to SYNACK from:

    gtg93 (25th June 2014)

  10. #7
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    593
    Thank Post
    99
    Thanked 135 Times in 119 Posts
    Rep Power
    49
    Quote Originally Posted by SYNACK View Post
    Beat me to it, at the moment it is populating those variables with nothing as it runs when the form is created and the user has not yet entered anything in so the value is null. By putting it in the onClick handler it only runs when the user has (hopefully) added the required information.
    Only because i was too lazy to explain why

    Hopefully that will sort you out @gtg93

  11. #8
    gtg93's Avatar
    Join Date
    May 2010
    Posts
    316
    Thank Post
    183
    Thanked 41 Times in 33 Posts
    Rep Power
    15
    That's done the job, thanks... I thought setting it at the top would make it available to the whole code for the remove and default option I'm also going to add.

    Will the DIMs need adding to these buttons seperately too? Will they need a different name, for example:

    Add Button
    Dim SERVERNAME as string = me.textbox1.text
    Dim PRINTERNAME as string = me.textbox2.text

    Remove Button
    Dim SERVERNAME2 as string = me.textbox1.text
    Dim PRINTERNAME2 as string = me.textbox2.text

    Default Button
    Dim SERVERNAME3 as string = me.textbox1.text
    Dim PRINTERNAME3 as string = me.textbox2.text




    edit:

    Quote Originally Posted by SYNACK View Post
    Beat me to it, at the moment it is populating those variables with nothing as it runs when the form is created and the user has not yet entered anything in so the value is null. By putting it in the onClick handler it only runs when the user has (hopefully) added the required information.
    I was populating the text boxes with some default text, so I thought this wouldn't cause an issue?

    Sorry for the million questions, just trying to get my head around it.
    Last edited by gtg93; 25th June 2014 at 03:35 PM.

  12. #9

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,139
    Thank Post
    860
    Thanked 2,692 Times in 2,282 Posts
    Blog Entries
    9
    Rep Power
    771
    Yes they will need to be added but no you won't need separate names as the variables are local to the handlers and are not seen outside them. The better - code reuse - way would be to have a subroutine that you run from the handlers. This would be passed a variable like add/remove/default then the one bit of code could use a case statement to pick which actions to perform from the variable
    Code:
    psudocode:
    private sub PrinterAction (Action as integer)
    Dim SERVERNAME as string = me.textbox1.text
     Dim PRINTERNAME as string = me.textbox2.text
    'run checks on the two variables and Action to make sure they are not null or have invalid characters, maybe even ping the servername
    'Dim objNetwork 'you'll need it whatever statement is run
    objNetwork = create...
    
    Select Case Action
        Case 1
            objNetwork.add...
           msgbox
        Case 2
            'delete printer
                 objNetwork.del...
           msgbox
      Case 3
           'default
        Case Else
            'something has gone wrong
    End Select
    
    end sub
    then PrinterAction(1) or whatever from the button click


    personally I'd pass the two textbox values directly in the routine to make it more reusable so Printeraction(1,TB1.value,TB2.value) with

    private sub PrinterAction (Action as integer, PrinterName as String, ServerName as String)
    and ditch the extra lines which are only a little useful for debugging.

    But that's probably enough to make your head spin for now
    Last edited by SYNACK; 25th June 2014 at 03:55 PM.

  13. 2 Thanks to SYNACK:

    gtg93 (25th June 2014), mac_shinobi (25th June 2014)

  14. #10
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    593
    Thank Post
    99
    Thanked 135 Times in 119 Posts
    Rep Power
    49
    If you want to set it at the top you can but you need to change the code slightly

    Code:
    Private SERVERNAME as string = nothing
    Private PRINTERNAME as string = nothing
    This will set those variables when the application is launched - you can then reference them in your functions later on

    For Example:

    Add Button
    Code:
    SERVERNAME = me.textbox1.text
    PRINTERNAME = me.textbox2.text
    
    'do something
    Once you have assigned a variable to the declaration you can reuse it so doing it on the add button may not be the best place if you intend using it (and add is not the first button called)

    You may want to look at updating the variable on LostFocus (an event that will run when the control loses focus)

  15. Thanks to sister_annex from:

    gtg93 (25th June 2014)

  16. #11

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,754
    Thank Post
    3,265
    Thanked 1,052 Times in 973 Posts
    Rep Power
    365
    To try and eliminate user input error and as you will know what printers are on what servers etc - surely it would be better to use drop down boxes to select which printer they want to add and from there you could add the relevant printer ( should know which printer is on which server so they shouldn't need to type the server name etc ) ??

    Drop down gets populated with all the printers in that department or all the possible printers they can add and they click on the add button which then adds the printer they had or have selected from said drop down menu ?
    Last edited by mac_shinobi; 25th June 2014 at 03:58 PM.

  17. #12
    gtg93's Avatar
    Join Date
    May 2010
    Posts
    316
    Thank Post
    183
    Thanked 41 Times in 33 Posts
    Rep Power
    15
    Quote Originally Posted by SYNACK View Post
    Yes they will need to be added but no you won't need separate names as the variables are local to the handlers and are not seen outside them. The better - code reuse - way would be to have a subroutine that you run from the handlers. This would be passed a variable like add/remove/default then the one bit of code could use a case statement to pick which actions to perform from the variable
    Code:
    psudocode:
    private sub PrinterAction (Action as integer)
    Dim SERVERNAME as string = me.textbox1.text
     Dim PRINTERNAME as string = me.textbox2.text
    'run checks on the two variables and Action to make sure they are not null or have invalid characters, maybe even ping the servername
    'Dim objNetwork 'you'll need it whatever statement is run
    objNetwork = create...
    
    Select Case Action
        Case 1
            objNetwork.add...
           msgbox
        Case 2
            'delete printer
                 objNetwork.del...
           msgbox
      Case 3
           'default
        Case Else
            'something has gone wrong
    End Select
    
    end sub
    then PrinterAction(1) or whatever from the button click


    personally I'd pass the two textbox values directly in the routine to make it more reusable so Printeraction(1,TB1.value,TB2.value) with

    private sub PrinterAction (Action as integer, PrinterName as String, ServerName as String)
    and ditch the extra lines which are only a little useful for debugging.

    But that's probably enough to make your head spin for now


    Thanks - I'll have to have another look at that bit later... I recognise your words but they're meaning nothing to me at the moment
    Last edited by gtg93; 25th June 2014 at 04:05 PM.

  18. #13

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,754
    Thank Post
    3,265
    Thanked 1,052 Times in 973 Posts
    Rep Power
    365
    Quote Originally Posted by gtg93 View Post
    Thanks - I'll have to have another look at that bit later... I recognise your words but they're meaning nothing to me at the moment
    How come printers aren't done via GPO or GPP ??

  19. #14
    gtg93's Avatar
    Join Date
    May 2010
    Posts
    316
    Thank Post
    183
    Thanked 41 Times in 33 Posts
    Rep Power
    15
    Quote Originally Posted by mac_shinobi View Post
    To try and eliminate user input error - surely it would be better to use drop down boxes to select which printer they want to add and from there you could add the relevant printer ( should know which printer is on which server so they shouldn't need to type the server name etc ) ??

    Drop down gets populated with all the printers in that department or all the possible printers they can add and they click on the add button which then adds the printer they had or have selected from said drop down menu ?
    I've already created one similar to how you're suggesting for staff:
    Printers.png

    I'm trying to put a GUI to a number of scripts I'd use regularly for my own use across sites... Still learning VB, so creating little things like this to pick up the basics.


    Quote Originally Posted by mac_shinobi View Post
    How come printers aren't done via GPO or GPP ??
    They are at most sites I support, it's just again, me trying to put some things together to pick up VB... We do have couple of sites who still map via script so might also be helpful.

    Plus some staff might be working on their laptop somewhere else in school for a day (working in a high school mainly), so we give them the above GUI to add the closest printer whilst they are, then they can easily remove it. We monitor printing with PaperCut, so no ones bothered about sharing deparment printers with other staff.
    Last edited by gtg93; 25th June 2014 at 04:04 PM.

  20. Thanks to gtg93 from:

    mac_shinobi (25th June 2014)

  21. #15

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,139
    Thank Post
    860
    Thanked 2,692 Times in 2,282 Posts
    Blog Entries
    9
    Rep Power
    771
    Quote Originally Posted by gtg93 View Post
    I was populating the text boxes with some default text, so I thought this wouldn't cause an issue?

    Sorry for the million questions, just trying to get my head around it.
    It is all to do with when the code is executed, that place in the code is executed before the form has even been created on screen so the default values have not yet been assigned, you could use the onLoad handler which should mean the buttons exist by that stage but the other ways discussed are better and much cleaner.

  22. Thanks to SYNACK from:

    gtg93 (26th June 2014)

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Visual basic 6
    By Halfmad in forum Windows
    Replies: 9
    Last Post: 11th June 2007, 02:57 PM
  2. visual basic 2005 beta 2 tutorials ?
    By mac_shinobi in forum Coding
    Replies: 4
    Last Post: 14th December 2006, 09:43 PM
  3. Visual Basic 2005 Express
    By Mintsoft in forum Windows
    Replies: 2
    Last Post: 9th June 2006, 08:21 AM
  4. LDAP & Visual basic 2005 ??
    By mac_shinobi in forum Coding
    Replies: 9
    Last Post: 10th January 2006, 10:13 PM
  5. Best manuals for Visual Basic .NET?
    By kingswood in forum Coding
    Replies: 6
    Last Post: 3rd October 2005, 10:41 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •