Jump to content
The mkiv Supra Owners Club

MS Access / VB help


Digsy

Recommended Posts

Hopefully a quickie.

 

I want to read the value from a field in a form into a variable in a VB code but I'm not a VB expert and the help files are rubbish.

 

I've defined the variable using

DIM MYVARIABLE AS STRING

 

Now I guess I have to use the SET command to read the field name, but that's where I get stuck!

 

Cheers,

Link to comment
Share on other sites

It's been a long while since I touched Access but I know VB pretty well,

 

dim VariableName as string

 

will indeed allocate memory for a string of text (assuming that is what you are reading from the form), then you will need to reference a component on the form to extract the value. For example if you have a textbox on the form called text1 you could do

 

VariableName = text1.text

 

This assigns the textbox called text1's 'text' property which is (as you would expect) the text within it

 

Similarly you could have:

 

dim Length OfText as integer

 

LengthOfText=Text1.length

 

This puts the size of the text in a variable which is an integer

Link to comment
Share on other sites

Thanks. How do I make sure it looks at the right form?

 

*edit* I'll expilain myself a bit better. :)

 

I want to get the data from a text field in the form, so I guess I'll have to use the Value property. The bit I'm having problems with is how to point the variable at the right form.

 

Let's say the form is called "MyForm" and the field is called "MyFeild".

 

I'm expecting to have to use something like:

 

Set MyVariable = MyDatabase.Forms.MyForm.MyFeild.Value

 

Or…

 

If the form is open and has focus (which will be the case), maybe VB is smart enough to just use

 

Set MyVariable = CurrentForm.MyField.Value (or some such).

 

Or maybe even just

 

Set MyVariable = MyField.Value

Link to comment
Share on other sites

If the VB is being written behind the form itself, then you should be able to say

 

MyVariable = me.myfield

 

('me' is the form referring to itself)

 

No need to set focus on the field as I do this all the time with hidden fields to which you can never pass the focus.

 

I'm assuming we're talking VBA for access here?

Link to comment
Share on other sites

If the VB is being written behind the form itself, then you should be able to say

 

MyVariable = me.myfield

 

('me' is the form referring to itself)

 

No need to set focus on the field as I do this all the time with hidden fields to which you can never pass the focus.

 

I'm assuming we're talking VBA for access here?

 

At the moment I'm writing the code in a seperate module so I think I need to create the full path to the field. However, I could attach it specifically to a button click on the form.

 

Yep, its VBA for Access (2002 I think). I'll have more of a play tonight (when the rest of you are all out getting drunk and enjoying yourselves - suckers ;) )

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. You might also be interested in our Guidelines, Privacy Policy and Terms of Use.