Digsy Posted March 11, 2010 Share Posted March 11, 2010 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, Quote Link to comment Share on other sites More sharing options...
JSeaman Posted March 12, 2010 Share Posted March 12, 2010 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 Quote Link to comment Share on other sites More sharing options...
Digsy Posted March 12, 2010 Author Share Posted March 12, 2010 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 Quote Link to comment Share on other sites More sharing options...
Digsy Posted March 12, 2010 Author Share Posted March 12, 2010 Ah. I think I've sussed it, courtesy of the interweb: Set MyVariable = Forms!MyForm!Myfield.Value Quote Link to comment Share on other sites More sharing options...
Ian C Posted March 12, 2010 Share Posted March 12, 2010 You might have to .setfocus the control first before you can read info from it Quote Link to comment Share on other sites More sharing options...
Soop Dogg Posted March 12, 2010 Share Posted March 12, 2010 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? Quote Link to comment Share on other sites More sharing options...
Digsy Posted March 12, 2010 Author Share Posted March 12, 2010 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 ) Quote Link to comment Share on other sites More sharing options...
Soop Dogg Posted March 12, 2010 Share Posted March 12, 2010 I was thinking more that you might be adding it as a sub in the VB editor. (Alt + F11 from your form, and then look for your form name under 'Microsoft Office Access Class Objects') Quote Link to comment Share on other sites More sharing options...
Digsy Posted March 13, 2010 Author Share Posted March 13, 2010 All sorted now. Set MyVariable = [Forms]![MyForm]!Myfield.Value Not sure why I have to include the square brackets, but it works. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.