'Change Event' trouble

G

Guest

Hi,
I am fairly new to Access Vbasic programming.

What I am trying to do is have the 'Change Event' fire when
I enter any text into a text box. The 'Change Event' calls the
code but a problems arises when I start to look at the text entered.

The problem seems to be that the text read in is alway 1 key stroke
behind what is in the text box. For example:
1 - I enter "e" into the text box. The text box contains "e", but the code
detects
a null string when reading the text box contents
2 - I then enter "f", now the text box contains "ef", but the code detects
only "e"
when reading the text box contents
3 - I then enter "f", now the text box contains "eff", but the code detects
only "ef"
when reading the text box contents

What am I missing? Can any one shed some light on this?
Any help is greatly appreciated.
 
B

Brendan Reynolds

Are you looking at the Text property or the Value property of the text box
control? If you're referring to the control by name without specifying a
specific property, you're looking at the Value property. Unlike VB controls,
the Value property is the default property of an Access form control.

The following code displays the text as I enter it ...

Private Sub Text0_Change()

Me.Label2.Caption = Me.Text0.Text

End Sub
 
G

Guest

I didn't know there was more than the text property.

Thanks for the tip, I will try applying what you said.
 
G

Guest

Your suggestion works nicely. This leads to another question related to the
1st topic.

Now that I can process the actual text in the text box, I use that info to
find
a record in an underlying table. On the form I am using there is a list box
below
the text box. I want to see in the list box a high-lighted selection which
represents
the information found in the selected record in the underlying table.

How can I get the list box to highlight the record's information?
 
G

Guest

I forgot to mention in this in my last post. Below is the code I use to read
in
the value entered into the text box.

strMatchText = Nz([Forms]![frmGetSubDivInfo]![txtMatchText].Text, "- - - - -")

When the form opens that contains the text box and list box, a new error
message
appears saying "Object doesn't support this property or method". Is the above
express causing this?
 
G

Guest

Hi,

I found the source of the error, I was trying to repaint a label.
Sorry about that. But the high lighting a list box value still stands.
 
B

Brendan Reynolds

A potential 'gotcha' to watch out for when using the Text property - you can
only use it when the control has the focus. This isn't a problem in the
Change event, as the control will have focus when that event fires, but it
can be confusing if you try to use the Text property in other events when
the control does not have focus.

Re highlighting an item in the listbox. For the purposes of this example,
I'm going to assume that this is not a multi-select listbox, and that the
bound column of the listbox matches the primary key field of the record.
Given those assumptions, here's an example using the Employees table from
the Northwind database. This example selects a record in the listbox
matching the 'last name' entered in the text box. 'Text0' is the name of the
textbox, and 'List2' is the name of the listbox.

Private Sub Text0_AfterUpdate()

Dim rst As ADODB.Recordset

Set rst = New ADODB.Recordset
With rst
.ActiveConnection = CurrentProject.Connection
.Source = "SELECT EmployeeID, LastName, FirstName " & _
"FROM Employees WHERE LastName = '" & _
Replace(Me.Text0, "'", "''") & "'"
.Open
If .BOF And .EOF Then
.Close
MsgBox "No match found."
Else
Me.List2 = .Fields("EmployeeID")
.Close
End If
End With

End Sub
 
G

Guest

Thanks for the tip I 'll look into it

Brendan Reynolds said:
A potential 'gotcha' to watch out for when using the Text property - you can
only use it when the control has the focus. This isn't a problem in the
Change event, as the control will have focus when that event fires, but it
can be confusing if you try to use the Text property in other events when
the control does not have focus.

Re highlighting an item in the listbox. For the purposes of this example,
I'm going to assume that this is not a multi-select listbox, and that the
bound column of the listbox matches the primary key field of the record.
Given those assumptions, here's an example using the Employees table from
the Northwind database. This example selects a record in the listbox
matching the 'last name' entered in the text box. 'Text0' is the name of the
textbox, and 'List2' is the name of the listbox.

Private Sub Text0_AfterUpdate()

Dim rst As ADODB.Recordset

Set rst = New ADODB.Recordset
With rst
.ActiveConnection = CurrentProject.Connection
.Source = "SELECT EmployeeID, LastName, FirstName " & _
"FROM Employees WHERE LastName = '" & _
Replace(Me.Text0, "'", "''") & "'"
.Open
If .BOF And .EOF Then
.Close
MsgBox "No match found."
Else
Me.List2 = .Fields("EmployeeID")
.Close
End If
End With

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top