Results 1 to 9 of 9

Thread: Basic Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2012
    Posts
    209

    Question Basic Question

    Dear Reader,

    This may seem like a pointless question but it's pure curiosity. I'm going over some tutorials on basic stuff like using Form_Load () to simply Print "Hello World" onto a Form. The code I stumbled upon is this,

    Code:
    Private Sub
    A = Tom
    B = "likes"
    C = "to"
    D = "eat"
    E = "burgers"
    Print A+B+C+D+E
    
    End Sub
    The code by itself does not work. Originally because it doesn't have anything after Sub, an identifier I believe it's called. Also, the quotations are unnecessary. The only one that is necessary to have a quotation is to because it's an ambiguous term. Overall the code doesn't work. So I'm trying to change it into something that DOES work. Here's what I came up with:

    Code:
    Private Sub Form_Load ()
    A = Tom
    B = likes
    C = eating
    D = burgers
    
    Print A + B + C + D
    
    End Sub
    However that doesn't work either. I tried
    Code:
    Dim A As String
    But that doesn't work either. Obviously, I Dim'd B..C..D also. I would like to know what I am doing wrong, why it's the way it should be (like what makes it correct), and what it should be.

    Again, this is for learning purposes only because I'm curious about Visual Basic 6 immensely.

    Thank you for taking the time to read this message.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Basic Question

    A Sub must have a name and must be called by something

    Literal string values must be enclosed in "s otherwise the engine thinks you are talking about a variable which of course has no value so you get no output.

    The way you are using + in the last line while it would work the preferred method is to use & to concantonate strings and use + to add numbers.

    A working piece of code would look like this

    Code:
    Private Sub Command1_Click()
    Dim A As String
    Dim B As String
    Dim C As String
    Dim D As String
    
    A = "Tom "
    B = "likes "
    C = "eating "
    D = "burgers"
    
    Print A & B & C & D
    
    End Sub
    This would require a command button with the default name of command1 be on the form and the code would execute when the button is clicked. Of course you could place the contents of the sub in another sub or function and get the same result.

    Also notice that I added a space to the end of the first few words so it would not print out as just a string of run on letters.

  3. #3
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Basic Question

    I ran DataMiser's code and it worked perfectly under the Command1.Click event. I moved the code directly to the Form_Load event and it didn't work.

    it seems some things can't run directly from Form_Load.

    I then made a private sub, put the code in that and called that from Form_Load, that didn't work either.

    So, the problem seems to be with what is and isn't allowed in Form_Load. After some trawling on the Net, I discovered that only initialisation stuff can be done in the Form_Load event because the form is not fully loaded until the event has ended. So setting variables, printint on the form etc won't work.
    Last edited by Españolita; May 30th, 2012 at 06:29 AM.

  4. #4
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: Basic Question

    There are a few things that should not be done in Form_Load, and I am pretty sure that Print to Form is one of them.
    You could use Form_Activate

    Or place a Label or Picturebox on the Form, and send your output to them

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

    Re: Basic Question

    Quote Originally Posted by españolito View Post
    So, the problem seems to be with what is and isn't allowed in Form_Load. After some trawling on the Net, I discovered that only initialisation stuff can be done in the Form_Load event because the form is not fully loaded until the event has ended. So setting variables, printint on the form etc won't work.
    None of that is true - but I can understand why you would have misinterpreted.

    The problem is to do with the .Print command and the settings of the object you are printing to.

    If the object is Visible (which can be achieved by adding Me.Show just before Print) then it will work, but whatever has been printed will disappear if you later hide the form and show it again.

    If the .Redraw property is True, then what has been printed will not disappear in the same way.


    Bobbles's suggestion of using a Label instead is a nice easy solution (but a PictureBox would have the same problems as printing to the form).

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2012
    Posts
    209

    Re: Basic Question

    Dear Si,

    I noticed you used the term
    .Redraw property
    Where would I find that, and how would I use that? Say given the example, the Printing on a Form.

  7. #7
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: Basic Question

    Si,
    I was not intending the Picturebox printing in the Form_Load.
    I am very wary of what code I place in the Form_Load event.

    I use the Form_Activate_Event instead, most times.
    I have a Boolean, that gets set to true, during the first activation, and some of my code then goes into the 'If' test on that flag, to ensure that it does not run more than once.
    Last edited by Bobbles; May 30th, 2012 at 01:26 PM. Reason: Extra info

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

    Re: Basic Question

    I actually meant .AutoRedraw rather than .Redraw, so following what I described would be this:
    Code:
    Me.Show
    Me.AutoRedraw = True
    Print A & B & C & D
    Using a Label instead would eliminate the need for that extra code, you could just have:
    Code:
    Label1.Caption = A & B & C & D
    Quote Originally Posted by Bobbles View Post
    I use the Form_Activate_Event instead, most times.
    I have a Boolean, that gets set to true, during the first activation, and some of my code then goes into the 'If' test on that flag, to ensure that it does not run more than once.
    It is a matter of opinion, but to me that is wasted effort (and ignorable waste of memory etc for the extra variable).

    Form_load is fine for the vast majority of things, and a simple Me.Show as above fixes most of the rest.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Basic Question

    I agree, I almost never use the form activate event. I do keep most code out of the load event as well but some code needs to be there and as said above a simple me.show or me.visible=true will usually do the trick in allowing you to draw on the form or set focus to an object. The exception being if the form was called in modal mode me.show or me.visible=true will cause an error and can not be used in the load event.

Tags for this Thread

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