Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get values from a textbox without setting focus. 2

Status
Not open for further replies.

Dustman

Programmer
May 7, 2001
320
US
Is there a way to get a value from a control without having to set the focus to it first or is there a reason Microsoft took this ability out of VBA?
 

You should be able to get the value without focus. In a module on the current form reference me.control.

Example: MsgBox Me.controlname.value ' value is optional

From one form you can reference another open form using the forms collection.

Example: MsgBox Forms!frmname!controlname Terry
------------------------------------
Blessed is the man who, having nothing to say, abstains from giving us worthy evidence of the fact. -George Eliot
 
I've tried that already. I know it works like that in VB but VBA isn't letting me do it. Anytime I try to referance a value, I get an error messege saying "You can't reference a property or method for a control unless the control has the focus."

I've been able to get around it a few times in my procedures by just setting the focus before I get the value but there is no way to make it do two at a time that way ie: strName = me!txtFName.text & me!txtLName.text -Dustin
Rom 8:28
 

What version of Access are you running? I did just as I posted in Access 2000. No need to set focus in VBA.

I suppose you could set focus, store the value in a variable, set focus to the other text box and concatenate the value to the stored value. Terry
------------------------------------
Blessed is the man who, having nothing to say, abstains from giving us worthy evidence of the fact. -George Eliot
 
Dustin

Terry's advice should also work in Access 97. Can you post the code you are trying to run so we can look at it?

Lightning
 
Well, I'm using Access 2000 too... It always gives me that same error. I've tried over and over to figure out why. Here is an example of one of my procedures..

Code:
strSearchFor = Trim(txtSearch.Text)
strSearchIn = Trim(cboSearchIn.Text)

lstResults.RowSource = "SELECT ShortSearch.LastName, ShortSearch.FirstName, ShortSearch.MI, ShortSearch.Suffix, ShortSearch.DOB, [ShortSearch.Patient#] FROM ShortSearch WHERE " & strSearchIn & " LIKE '" & strSearchFor & "*' ORDER BY LastName;"

It only works if I do this...

Code:
txtSearch.SetFocus
strSearchFor = Trim(txtSearch.Text)
cboSearchIn.SetFocus
strSearchIn = Trim(cboSearchIn.Text)

lstResults.RowSource = "SELECT ShortSearch.LastName, ShortSearch.FirstName, ShortSearch.MI, ShortSearch.Suffix, ShortSearch.DOB, [ShortSearch.Patient#] FROM ShortSearch WHERE " & strSearchIn & " LIKE '" & strSearchFor & "*' ORDER BY LastName;"

Like I said.. Access has always given me this error.. I can't figure out why.

-Dustin
Rom 8:28
 
Hi,

Without first trying this, but the .text property is possibly only valid if the control has focus as this is the 'new' value. Try the control reference without the .text reference.

Cheers

Steve
 
Well, I tried that but now I get "Invalid use of null" error when it tries to set the variable. I tried it without using the variables and nothing happens in my SQL statement. -Dustin
Rom 8:28
 

I agree. The .text seems to cause the problem. I've finally been able to duplicate the error with .text appended to the control name. You can use txtSearch.value or just txtSearch as I mentioned in an earlier post. Terry
------------------------------------
Blessed is the man who, having nothing to say, abstains from giving us worthy evidence of the fact. -George Eliot
 
HI,

This seems to be getting messy, but:
The null message is that the combo (field names?) is not populated, so this variable might require the .text attribute (unless this value is based on a table). The value of the string you are searching for should not matter (as you have the *). This has a lot to do with from where you are firing this action from and how (the combo onChange?).

I would suggest pulling back for a minute. Hardcode the criteria field, and see if this works without the .text attribute for the value string (I think it will). Then start looking at dynamically setting the field criteria. This might require a nz() function call to include the where clause.

Good luck & cheers
 
Hey thanks a lot guys.. I finally got it all working. I can get it to work by using the setfocus on the txtSearch and then just getting the value from the combobox. About the null thing.. its the txtSearch.value that is returning null. The combobox's source is a field list from a table so it is "populated" if I understand the terminology. It seems to work fine. The event is actually fired on the keyup procedure of the txtSearch. That way it narrows the list as they type. This is the final working code..

Code:
txtSearch.SetFocus
strSearchFor = txtSearch.Text
strSearchIn = cboSearchIn.Value

lstResults.RowSource = "SELECT ShortSearch.LastName, ShortSearch.FirstName, ShortSearch.MI, ShortSearch.Suffix, ShortSearch.DOB, [ShortSearch.Patient#] FROM ShortSearch WHERE " & strSearchIn & " LIKE '" & strSearchFor & "*' ORDER BY LastName;"

Although I got it working.. I'm still curious about why I can never get a value out of a textbox using the text property without the setfocus. Why does the .value property return null?

Thanks for all the help! -Dustin
Rom 8:28
 
From the help:

While the control has the focus, the Text property contains the text data currently in the control; the Value property contains the last saved data for the control. When you move the focus to another control, the control's data is updated, and the Value property is set to this new value. The Text property setting is then unavailable until the control gets the focus again. If you use the Save Record command on the Records menu to save the data in the control without moving the focus, the Text property and Value property settings will be the same.

Reading this now suggests that the .value property was probably what you were after. Live & learn

Cheers
 
That clears up a whole lot. Thanks! I've got alot of cleaning up to do in my other procedures now.. Part of my problems comes from staying up so late at night working on it. :)

All of yall have been extrememly helpful. Thanks a million!! -Dustin
Rom 8:28
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top