Link to home
Start Free TrialLog in
Avatar of jbakestull
jbakestull

asked on

Access 2007 SQL txt field Question

I'm trying to understand how to extract data from a query using SQL to form.

Question: Can you assign a query value to a text field?  or does it always have to be a list box?

Me.txtCity.RowSource = vcatch  returns a value

Me.txtCity2.Value = vcatch returns the SQL statement.


Private Sub Command5_Click()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim vcatch As String
Dim vsql As String


If IsNull(Me.txtsearch) = True Then
    MsgBox "Please entered a client ID", vbOKOnly
Exit Sub
Else

vsql = Me.txtsearch

strSQL = "SELECT Table1.City " & vbCrLf & _
"FROM Table1 " & vbCrLf & _
"GROUP BY Table1.CompnayID, Table1.City " & vbCrLf & _
"HAVING (((Table1.CompnayID)='" & vsql & "'));"




vcatch = strSQL

Me.txtCity.RowSource = vcatch
Me.txtCity2.Value = vcatch
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

If Not (rst.EOF And rst.BOF) Then
      'display your records
              '........................
Else ' if no records found
      MsgBox "No records found!", vbOKOnly

    Set rst = Nothing
    Set db = Nothing
    
End If
End If
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The code you posted won't even compile because of

Me.txtCity.RowSource = vcatch

assuming txtCity IS really a Text box ...?

mx
Avatar of jbakestull
jbakestull

ASKER

txtcity is a list box

txtcity2 is a test box

I switched the text box type not the naming. Sorry for the confusion.

Could you provide a example on how to use

=DLookup("[YourFieldName]", "YourQueryName", "<OptionalCriteria>")?
ok.. What value/field ... exactly are you trying to assign to the text box ?

mx
City value based value entered into txtsearch, which contains the company ID value from table1

query is named query1
ok ... so, you question was "Can you assign a query value to a text field?"

So, what is the name of the Field in query1 that you want to assign to the text box ?

mx
City


=DLookup("[City]", "Query1")

Now, this assumes that query1 is only returning one record.  If not, then you will need to apply some criteria ...

mx
The query does return one record.

So if the query has multiple fields, your able to choose the field you want using "<OptionalCriteria>"?
"So if the query has multiple fields, your able to choose the field you want using "<OptionalCriteria>"?"
Ummm ... no.  You specify the field in the first argument of the DLookup().  If you only have one record (good) .... then no need for criteria argument ...

mx
Thanks for your help