Showing data retrieved from a database using a query

  • Thread starter Thread starter Judicator
  • Start date Start date
J

Judicator

Hello,

Let's say I have a table with just two columns. One contains the name
and the other contains the addresses. I have a form where I have a
text box where someone would key in the name. I also a control
button, which if clicked on, will show the corresponding address in
another text box (or is it possible to show it on a label?).

Now the question is how do I do it? I can show the values by opening
the datasheet, which I don't want to. I want to print the values on
the same form that is being used to search for that name and address.

I am an absolute newbie when it comes to Access. I couldn't find
anything useful in the help files.

So any kind help would be highly appreciated.

Thanks!


-- Judicator
 
You can use the DLookup function in the AfterUpdate event of the textbox. Or
you can use a combobox to display the name with an additional column for the
address. Use the AfterUpdate event of the combobox to set a label's caption
property. Something like:

Sub MyCombo_AfterUpdate()
Me.Label1.Caption = Me.MyCombo.Column(1)
End Sub

Column(1) is the index value of the second column in a combobox.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
There's an example below. Note that this assumes that names are unique,
which is rarely a safe assumption. In Access 2000 or 2002, you'll need to
add a reference to the Microsoft DAO 3.6 Object Library if it is not already
referenced (earlier and later versions add this reference by default).

Private Sub TheButton_Click()

Dim db As DAO.Database
Dim rst As DAO.Recordset

'Make sure the user typed something in the text box.
If Len(Trim$(Me!TheTextBox & vbNullString)) > 0 Then
Set db = CurrentDb
'Open recordset allowing for possibility of apostrophes in names.
Set rst = db.OpenRecordset("SELECT TheAddress FROM tblTest WHERE
TheName = '" & _
Replace(Me!TheTextBox, "'", "''", 1, -1, vbBinaryCompare) & "'")
'If a match is found ...
If Not (rst.BOF And rst.EOF) Then
'Allow for possibility that field may be Null.
Me!TheLabel.Caption = rst.Fields("TheAddress") & vbNullString
End If
rst.Close
End If

End Sub

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Back
Top