Results 1 to 7 of 7

Thread: Beginner's Quandry

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    11

    Beginner's Quandry

    Hey guys, I've been programming VBA for a while now, and I recently started developing straight VB appliactions for some peers. However, I must admit my VBA knowledge is much more extensive than my VB knowledge, and I've run into a few problems (common beginner problems, I'm sure). On any note, I have some code that I've attached below:

    Code:
    Public gblUserID 'String
    Public gblUserPass 'String
    Public gblAttackType 'Integer
    Public gblAttackWho 'Integer
    
    Public Sub Main()
    
    	'The main area of the code, this is where you enter.
    	'Start by asking the user some questions - put some error checking here to make sure they enter valid options
    	On Error Resume Next
    	
    	Call GetUserInputs
    	
    	If (Err.Number <> 0) Then
    		MsgBox "Your enteries were invalid.  Please re-run the program and try again."
    		Exit Sub
    	End If
    	
    	On Error Goto 0
    	
    
    End Sub
    
    Private Sub GetUserInputs()
    
    	'Create the opening pop-up box            
    	Set startIE = CreateObject("InternetExplorer.Application")
    	
    	'Set that start box defaults
    	startIE.Navigate "about:blank"
    	startIE.ToolBar = False
    	startIE.Resizable = False
    	startIE.StatusBar = False
    	startIE.Width = 340
    	startIE.Height = 340
    	
    	'Set up the height and width
    	With startIE.Document.ParentWindow.Screen
    	    startIE.Left = (.AvailWidth - startIE.Width) \ 2
    	    startIE.Top = (.AvailHeight - startIE.Height) \ 2
    	End With
    	
    	'Loop until ready
    	Do While startIE.Busy: Wscript.Sleep 100: Loop
    	
    	'Create the inner HTML for the box
    	startIE.Document.Body.InnerHTML = "<DIV ALIGN=""left""><P>Mouse: 1 - Rat: 2 - Chicken: 3 - " _
    	    & "Dog: 4 - Cat: 5 - Bear: 6 - Dragon: 7</P>" & vbCrLf _
    	    & "<P>Monster Number: <INPUT TYPE=""text"" SIZE=""2"" ID=""monster""</P>" & vbCrLf _
    	    & "<P>1 for Strength, 2 for Defense: <INPUT TYPE=""text"" SIZE=""2"" ID=""attacktype""</P>" & vbCrLf _
    	    & "<P>Username: <INPUT TYPE=""text"" SIZE=""20"" ID=""username""</P>" & vbCrLf _
    	    & "<P>Password: <INPUT TYPE=""password"" SIZE=""20"" ID=""password""></P>" & vbCrLf _
    	    & "<P><INPUT TYPE=""hidden"" ID=""OK"" NAME=""OK"" VALUE=""0""><INPUT TYPE=""submit"" VALUE="" Submit "" " _
    		    & "OnClick=""VBScript:OK.Value=1""></P></DIV>"
    			
    	'Show the completed box
    	startIE.Visible = True
    	
    	'Keep looping until "Submit" is selected
    	On Error Resume Next
    	Do Until (startIE.Document.All.OK.Value = 1 Or startIE.Visible = False): Script.Wait 300: Loop
    	On Error GoTo 0
    	
    	'Grab all our values
    	gblAttackWho = CInt(startIE.Document.All.monster.Value)
    	gblAttackType = CInt(startIE.Document.All.attacktype.Value)
    	gblUserID = CStr(startIE.Document.All.username.Value)
    	gblUserPass = CStr(startIE.Document.All.password.Value)
    	
    	'Get rid of the start box
    	startIE.Quit
    	Set startIE = Nothing
    
    End Sub
    Now, in VBA I know I can have different routines, functions, and classes to call and utilize, but I don't know how to make it work for VB. I have created an executable from this code, but I think it just runs through the global variables and then stops. Is there a way to get the executable to start in a certain sub routine when it begins the run? Can I continue to use routines as I have in VBA? Any help is very much appreciated! Thanks!

  2. #2
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Beginner's Quandry

    Code:
    Sub Main()
    ,

    is modular code, if you add a module to your project then put your code in there, then goto project-->project properties and set the startup object to sub main() .


    Either that or put your code in a form like you probably have and change

    Code:
    Public Sub Main()
    to
    Code:
    Private Sub Form_Load()
    Also you can make your variables private.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    11

    Re: Beginner's Quandry

    What if I am working in VbsEdit?

  4. #4
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Beginner's Quandry

    Quote Originally Posted by dehboy View Post
    What if I am working in VbsEdit?

    Ah your writing a vbs, yuo just haven't called sub main, just add a

    Code:
    main()
    under the variables but above sub main().

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    11

    Re: Beginner's Quandry

    Well that seems ackward...but it worked, so thank you!

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Beginner's Quandry

    Quote Originally Posted by dehboy View Post
    What if I am working in VbsEdit?
    Thread moved from 'VB6 and Earlier' forum to 'ASP, VBScript' forum

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Beginner's Quandry

    Quote Originally Posted by dehboy View Post
    Well that seems ackward...but it worked, so thank you!
    It is awkward. There is no need for a Sub Main() in a WSH script, which is probably what you are making.

    When WSH invokes the script engine the "outer block" code is always run:
    Code:
    Option Explicit
    
    Dim Answer
    
    Answer = InputBox("Question")
    MsgBox "You answered " & Answer

Posting Permissions

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



Click Here to Expand Forum to Full Width