• 0

VBA: Access (Beginner)


Question

Howdy, just started doing some VBA with Access. I've done some Java and C/C++ so I'm not too newbie but they're some strange things here! :D

For an exercise, we have to make 2 forms in MS Access. The first form has a textbox and a command button. The second form has only a textbox. The objective is to:

- enter some value in Form1's textbox

- click the command button on Form1

- have it "set focus" onto Form2

- have the value magically appear on Form2

I'm sure this is very basic for most of you but I have some questions :D Here's my code anyway, it works but I got some help from a friend but don't fully understand it (yet)...

' Form1's Code
Private Sub Command_Click()
    textBoxInForm1.SetFocus
    Form_Form2.SetFocus
End Sub

' Form2's Code
Private Sub Form_Load()
    textBoxInForm2.SetFocus
    textBoxInForm2 = Form_Form1.textBoxInForm1.Text
End Sub

Q1) What is the whole idea behind this "SetFocus" thing?

Q2) VB doesn't require () after methods/functions?

Q3) While "Me" is not used here, I've seen it elsewhere in sample code. Is it the same as "this" in Java/C++?

Cheers for the help.

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

A1) SetFocus will set focus to the control of which you're calling the method. I'm not sure how to describe what focus is... I'll give some examples instead. When you have a bunch of windows open, the one that you clicked on last will have a different coloured caption bar because it has focus. Controls that have focus will have a dotted line going around or inside them (or for text boxes, the one with focus will have the blinking text cursor). Try pressing tab a few times to see what I mean.

A2) VB does not require () for methods or functions unless you are calling that method or function with parameters. An exception is when you want to call a function without using its output for anything (Ex. "Call BlahBlah 1, 2, 3" instead of "A = BlahBlah(1, 2, 3)").

A3) I'm not sure what "this" is, but they're probably the same thing. It's the parent object of wherever you're using it (ex. the form when you're writing code for a control that's on it).

A final note: SetFocus is very important in Access when you want to change the value of a text box or other control. You have to set focus to it first or it'll throw an error. Also, don't use "textBoxInForm2 = Form_Form1.textBoxInForm1.Text", instead be explicit and put "textBoxInForm2.Text = Form_Form1.textBoxInForm1.Text". This would be a bad habit if you ever want to move on to VB.Net since there are no default properties there (textBoxInForm2 would always refer to the textbox and not its text).

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.