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
--
Brendan Reynolds
Access MVP
"CityGuy" <(E-Mail Removed)> wrote in message
news:24171D44-85EE-4815-B3A7-(E-Mail Removed)...
> 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?
>
>
>
> "Brendan Reynolds" wrote:
>
>>
>> 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
>>
>> --
>> Brendan Reynolds
>> Access MVP
>>
>> "CityGuy" <(E-Mail Removed)> wrote in message
>> news:7F98D6F2-337A-4B7B-8698-(E-Mail Removed)...
>> > 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.
>> >
>> >
>> >
>>
>>
>>
|