gtg93 Posted June 25, 2014 Posted June 25, 2014 (edited) 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. 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. Edited June 25, 2014 by gtg93
sister_annex Posted June 25, 2014 Posted June 25, 2014 Sorry to elaborate: In this case I would do the following... Dim SERVERNAME as string = me.textbox1.text Dim PRINTERNAME as string = me.textbox2.text .... objNetwork.AddWindowsPrinterConnection("\\" & SERVERNAME & "\" & PRINTERNAME & ") HTH 1
gtg93 Posted June 25, 2014 Author Posted June 25, 2014 Thanks! - I should have thought of that, I've done that recently but still quite new to it... 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. Any ideas?
sister_annex Posted June 25, 2014 Posted June 25, 2014 Move the DIMs in to the button click event code 1
SYNACK Posted June 25, 2014 Posted June 25, 2014 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. 1
sister_annex Posted June 25, 2014 Posted June 25, 2014 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
gtg93 Posted June 25, 2014 Author Posted June 25, 2014 (edited) 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: 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. Edited June 25, 2014 by gtg93
SYNACK Posted June 25, 2014 Posted June 25, 2014 (edited) 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 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 Edited June 25, 2014 by SYNACK 2
sister_annex Posted June 25, 2014 Posted June 25, 2014 If you want to set it at the top you can but you need to change the code slightly 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 SERVERNAME = me.textbox1.text PRINTERNAME = me.textbox2.text 'do something [/Code] 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) 1
mac_shinobi Posted June 25, 2014 Posted June 25, 2014 (edited) 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 ? Edited June 25, 2014 by mac_shinobi
gtg93 Posted June 25, 2014 Author Posted June 25, 2014 (edited) 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 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 Edited June 25, 2014 by gtg93
mac_shinobi Posted June 25, 2014 Posted June 25, 2014 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 ??
gtg93 Posted June 25, 2014 Author Posted June 25, 2014 (edited) 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: 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. 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. Edited June 25, 2014 by gtg93 1
SYNACK Posted June 25, 2014 Posted June 25, 2014 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. 1
mac_shinobi Posted June 25, 2014 Posted June 25, 2014 (edited) I've already created one similar to how you're suggesting for staff: [ATTACH=CONFIG]25264[/ATTACH] I meant something similar to but neater than the below Button 1 would be renamed to Add Printer and the drop down would be pre populated with all the possible printers whether this is all the printers they can add around the school or just printers in the area they are currently at etc ?? Failing that if they select which printer they want to add you could just get your vba to open an explorer window to the relevant print server and they just double click on the printer they want to install which if setup correctly should push / install the drivers onto the client and add the printer ? If its Art Printer on print server FNPART01 then just a unc path to \\FNPART01 ..... Edited June 25, 2014 by mac_shinobi 1
SYNACK Posted June 25, 2014 Posted June 25, 2014 (edited) Possibly the Automagic version http://visualbasic.ittoolbox.com/groups/technical-functional/vb-dotnet-l/how-to-get-list-of-shared-printers-2181211 list of print servers plus a little extra code and it is dynamic. Edited June 25, 2014 by SYNACK 1
gtg93 Posted June 26, 2014 Author Posted June 26, 2014 I meant something similar to but neater than the below [ATTACH=CONFIG]25268[/ATTACH] Button 1 would be renamed to Add Printer and the drop down would be pre populated with all the possible printers whether this is all the printers they can add around the school or just printers in the area they are currently at etc ?? Failing that if they select which printer they want to add you could just get your vba to open an explorer window to the relevant print server and they just double click on the printer they want to install which if setup correctly should push / install the drivers onto the client and add the printer ? If its Art Printer on print server FNPART01 then just a unc path to \\FNPART01 ..... I see what you're saying... I've already knocked up the earlier one, so for now will leave it as is, but may come back and have a fiddle around with the drop down boxes at a later stage... Just figuring out what ou can can't do with it... Thanks for suggestions though Possibly the Automagic version How to get List of Shared Printers - Toolbox for IT Groups list of print servers plus a little extra code and it is dynamic. I'll have a look at this too - thanks. 1
mac_shinobi Posted June 26, 2014 Posted June 26, 2014 (edited) Not sure if this is correct but maybe create a function that you can pass parameters to so as to add the printer(s) depending on the value the user selects from the drop down PrintServerOne Printer_One Printer_Two Printer_Three PrintServerTwo Printer_Four Printer_Five Printer_Six Dim strSvrName As String Function InstallPrinter(byval strServer, byVal strShareName) 'Code to install / add the printer depending on selection made End Function Maybe use a select case so if they select printers that are on one print server it will know that they are on this print server versus if they select another printer which is on a different print server so something along the lines of Select Case cboSelection.Value Case 1 or 2 or 3: strSvrName = "PrintServer_One" InstallPrinter(strSvrName, cboSelection.Value) Case 4 or 5 or 6: strSvrName = "PrintServer_Two" InstallPrinter(strSvrName, cboSelection.Value) Case Else: MsgBox "Error message or wrong selection made etc" End Select With the 2nd parameter you may have to do a bit more to translate the selected item into the actual printer share name before you can call the function ?? I suppose it depends on how you are installing the printers on there machines though as to how you would get it to add or install the printer Edited June 26, 2014 by mac_shinobi 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now