Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 15th, 2005, 05:28 PM
theguz theguz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 54 theguz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 40 m 34 sec
Reputation Power: 4
Unhappy Please Help Updated

Thanks for the first response, I still get the same error. It says "Run-Time Error "2342":
A RunSQL action requires an argument consisting of an SQL statement. Here is my code. I thought my RunSQL statement had the argument. Please tll me what I am missing and doing wrong. Thanks :
Private Sub cmdResults_Click()
'Once button is clicked, search results will be displayed

Dim strField As String 'Field Name from listbox
Dim strText As String 'Data for filter from textbox
Dim strSQL As String 'Vari. for SQl string

strField = Form_Othersfrm!listFieldNames.Value
strText = Form_Othersfrm!txtData.Value

strSQL = "SELECT * FROM Results WHERE " & strField & " LIKE '" & strText & "';"


DoCmd.RunSQL (strSQL) 'Here is where the error appears!


End Sub

Last edited by theguz : June 16th, 2005 at 08:52 AM. Reason: updated

Reply With Quote
  #2  
Old June 15th, 2005, 06:01 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 4
Try this:

strSQL = "SELECT * FROM Results WHERE " & strField & " LIKE '" & strText & "';"

You can place a wild card to search on just the first few letters by using the '*' in the sql like below. That way you don't have to type in exactly the item you are searching on.

strSQL = "SELECT * FROM Results WHERE " & strField & " LIKE '" & strText & "*';"

lwells

Reply With Quote
  #3  
Old June 16th, 2005, 04:57 PM
theguz theguz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 54 theguz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 40 m 34 sec
Reputation Power: 4
Thanks for the reply but it still isn't working. I updated the question to show what the error is.

Reply With Quote
  #4  
Old June 17th, 2005, 11:02 AM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 4
First I will apologize for the previous post...the DoCmd.RunSQL only applies to action type queries, such as an append, update, make table or delete type queries.

What you are wanting to do is make a search and then review the results of that search. You will need to modify the code slightly.

There are two ways to go about this depending on how you want to display the results of your search.

(1) if you are using one form to enter the search information and then open another form based on the search results then do the following.

First create a query of the table you are searching on. Add all the fields to the query and save the query with an appropriate name. Next set the record source of the form to be opened that will display the results to this query.

Now add the following code behind your cmdResults command button:

Private Sub cmdResults_Click()

Dim qdf As QueryDef
Dim strField As String 'Field Name from listbox
Dim strText As String 'Data for filter from textbox
Dim strSQL As String 'Vari. for SQl string

Set qdf = CurrentDb().QueryDefs("NameOfQuery")

strField = Form_Othersfrm!listFieldNames.Value
strText = Form_Othersfrm!txtData.Value

strSQL = "SELECT * FROM Results WHERE " & strField & " LIKE '" & strText & "';"

qdf.SQL = strSQL
DoCmd.OpenForm ("NameOfForm")

End Sub

Use the name of your query and form in place where the text is in Bold above.

If you are using Access 2000 or higher, you may need to modify the above and set references to the DAO Object library and then change the code slightly to look in the DAO library if you get a compile error with the above code.

Dim db As DAO.Database
Dim qdf as DAO.QueryDef

Set db = CurrentDB()
Set qdf = db.QueryDefs("NameOfQuery")

Or

(2) If you are going to use the same form to display the results that has the Othersfrm!listFieldNames list box and the Othersfrm!txtData textbox you are searching with, then bound this form to your table and use the bookmark method to display the results.

Use the following code behind your command button as shown below:

Private Sub cmdResults_Click()

Dim strField As String 'Field Name from listbox
Dim strText As String 'Data for filter from textbox
Dim strSQL As String 'Vari. for SQl string

strField = Form_Othersfrm!listFieldNames.Value
strText = Form_Othersfrm!txtData.Value

strSQL = "SELECT * FROM Results WHERE " & strField & " LIKE '" & strText & "';"

Me.RecordsetClone.FindFirst strField & " Like '" & strText & "'"
Me.Bookmark = Me.RecordsetClone.Bookmark

End Sub

This will move the records to the one you were searching on.

Hope this explains how to make a search form for you. Post back if you have additional questions.

lwells

Reply With Quote
  #5  
Old June 17th, 2005, 11:31 AM
theguz theguz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 54 theguz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 40 m 34 sec
Reputation Power: 4
Talking

You are a genius. It works perfectly. Thank you very much. Now let me ask you a question, I don't know much about VBA is there a good book to learn from? Also please explain why you had to do this part:
Dim db As DAO.Database
Dim qdf As DAO.QueryDef

Set db = CurrentDb()
Set qdf = db.QueryDefs("qryResults2")
and
qdf.SQL = strSQL.

What does that suff do and mean. Once again thank you very much.

Reply With Quote
  #6  
Old June 17th, 2005, 11:59 AM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 4
There are several good books on Access, but honestly the one that I found very useful was Beginning VBA for Access 2000 and sorry I don't remember the authors off hand, but if you want I will dig it up and post here.

Basically and simply, beginning with Access 2000, two Object models were available. The DAO and the ADO librarys. While both are very similar each have their own syntax and methods, however the syntax is so similar between them that Access may not know which library to look in for any given snippet of code. Therefore to explicitly state which library to look in is sometimes required. The Set qdf made reference to the query collections in the current database and which query. The qdf.SQL refered to the sql of the query and then set the sql of the query to the strSQL that was stated.

Anyway, just look for any beginners book on access and you will be able to get a better understanding of the differences between ADO and DAO.

lwells

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft Access Development > Please Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway