PDA

View Full Version : [SOLVED:] SetFocus method...



krishnak
05-24-2010, 04:13 PM
Hi All,

We have a Form on Access 2007 with two Textboxes and a button and the Form operates on the following code:

Public Sub btnShMetrics_Click()
'Declare local variables for the form.
Dim pName As String, divNr As String
txtPName.SetFocus
pName = Me.txtPName.Text
MsgBox (pName)
txtDivNr.SetFocus
divNr = Me.txtDivNr.Text
MsgBox (divNr)
End Sub


If I want to read the value in any Textbox, I have to set focus on that Textbox. Obviously we can set focus only on one control at any instant.
After reading the value in the second Textbox(txtDivNr), I lost focus on the first Textbox (txtPName).
This means, in a long sequence of operations, before reading/writing the value in any control, the code has to precede by
[Control].SetFocus

Is VBA in Access always like that? Maybe I am mistaken, it used to be simpler. I do not remember of explicitly setting the focus on each control.

Or have I done anything wrong in setting up the Form and the controls? The 'tab' control is working OK when tested.
I inserted 'unbound' Textboxes in the Form.
I'll appreciate any advice.
Thanks in advance

OBP
05-25-2010, 04:04 AM
Well my first question is why are you using Unbound Text Boxes?
If they were bound to the table controls you would not need to do any "reading".
You do not normally need to Set Focus on a Bound or an Unbound Field to read it's value in VBA.
Have you tried using the simpler
pName = Me.txtPName
or even
pName = Me.txtPName.value

krishnak
05-25-2010, 05:39 AM
Thanks, OBP.
That did the trick. By using expression txtPName.Value instead of txtPName.Text, the code works well without the need to set focus on the control.
Now coming back to the use of Unbound text boxes, the table contains a large number of repetitive values of the variable in these columns. It will suit me if I can get the unique values from these fields and then bind the Textbox to the field. That will save me the trouble of writing the code.
Can you please direct me in the direction of getting the unique values from the field to populate the bound textbox?
I am also thinking of using a listbox where I can select more than one value for my query.

Thanks again

- Krishna

OBP
05-25-2010, 07:31 AM
Krishna, the Field Property "Control Source" binds the field to the table's Field. The form's property "Record Source" is used to bind the form to the table or query.