473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Search forms on MS Access database

Hi all,

I have a database that I've created a search form. Everything works great the only problem is the first time when the form is opened the search doesn't work, but after that it works. I've looked over and over at the code, there is nothing to trigger this problem. Can you guys think of something that I'm missing.

Thanks

Wana
Mar 28 '07 #1
18 1971
Rabbit
12,516 Expert Mod 8TB
What do you mean it doesn't work? What is it supposed to do that it's not doing?
Mar 28 '07 #2
What do you mean it doesn't work? What is it supposed to do that it's not doing?
Hi,

No matter what I select on my search screen the whole data shows up. So the search doesn't work. but if I use the same search criteria the second time it work right. For example I have 342 total records the first time with my search all 342 records shows up but second time with the same search criteria the correct amount of records 88 displays.

thanks for your help

Wana
Mar 28 '07 #3
Rabbit
12,516 Expert Mod 8TB
Did you put your code in the On Load event of the form?
Mar 28 '07 #4
Did you put your code in the On Load event of the form?
No I have a search button on the form. The form has several list boxes and text box searches. Once the user selects info they will click on the search button to find it which doesn't work the first time. But I do have this code on the my form load event

Me.txtEnd.Value = Date
strEndDate = Format(CDate(Me.txtEnd.Value), "\#m/d/yyyy\#")

this is just to have the end date automated to today's date.

Thanks

Wana
Mar 28 '07 #5
Rabbit
12,516 Expert Mod 8TB
So the first time they click the button it doesn't work but it works the second time they click the button? Did you use a Me.Refresh or a control.Requery at the end of the code?

If it still doesn't work, post your code so we can take a look at it.
Mar 28 '07 #6
So the first time they click the button it doesn't work but it works the second time they click the button? Did you use a Me.Refresh or a control.Requery at the end of the code?

If it still doesn't work, post your code so we can take a look at it.
I don't use any of these codes, here is the code:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Dim db As DAO.Database
  5. Dim qryDef As DAO.QueryDef
  6. Dim rst As Recordset
  7. Dim SQL As String
  8. Dim strStartDate As String
  9. Dim strEndDate As String
  10.  
  11. Private Sub cmdSearch1_Click()
  12. On Error GoTo Err_cmdSearch1_Click
  13.  
  14.     Dim i As Integer
  15.     Dim strSQL As String
  16.     Dim strWhere As String
  17.     Dim strIN As String
  18.     Dim strAut As String
  19.     Dim strChe As String
  20.     Dim strDoc As String
  21.     Dim strLan As String
  22.     Dim strOCR As String
  23.     Dim varItem As Variant
  24.  
  25.  
  26.     Set db = CurrentDb()
  27.  
  28.     strSQL = "SELECT * FROM FUELS"
  29.  
  30.     'Build the IN string by looping through the listbox
  31.     For i = 0 To lstCompany.ListCount - 1
  32.         If lstCompany.Selected(i) Then
  33.             strIN = strIN & "'" & lstCompany.Column(0, i) & "',"
  34.         End If
  35.      Next i
  36.     For i = 0 To lstDocType.ListCount - 1
  37.         If lstDocType.Selected(i) Then
  38.             strDoc = strDoc & "'" & lstDocType.Column(0, i) & "',"
  39.         End If
  40.      Next i
  41.      For i = 0 To lstLanguage.ListCount - 1
  42.         If lstLanguage.Selected(i) Then
  43.             strLan = strLan & "'" & lstLanguage.Column(0, i) & "',"
  44.         End If
  45.      Next i
  46.     'Create the WHERE string, and strip off the last comma of the IN string
  47.  
  48.     'Company - End Date
  49.     If lstCompany.ItemsSelected.Count <> 0 And Me.txtAuthor = "" And Me.txtChemical = "" And lstDocType.ItemsSelected.Count = 0 And lstLanguage.ItemsSelected.Count = 0 And Me.txtEnd <> "" Then
  50.         strIN = Left(strIN, Len(strIN) - 1)
  51.         'strEndDate = Format(CDate(Me.txtEnd.Value), "\#m/d/yyyy\#")
  52.         strWhere = " WHERE [Company] in (" & strIN & ") and [DOC_DATE] <= " & strEndDate & " "
  53.     'Company - OCR text - Start date - End date
  54.     ElseIf lstCompany.ItemsSelected.Count <> 0 And Me.txtAuthor = "" And Me.txtChemical = "" And lstDocType.ItemsSelected.Count = 0 And lstLanguage.ItemsSelected.Count = 0 And Me.txtOCR <> "" And Me.txtStart <> "" And Me.txtEnd <> "" Then
  55.         strIN = Left(strIN, Len(strIN) - 1)
  56.         'strEndDate = Format(CDate(Me.txtEnd.Value), "\#m/d/yyyy\#")
  57.         strStartDate = Format(CDate(Me.txtStart.Value), "\#m/d/yyyy\#")
  58.         strOCR = Me.txtOCR.Value
  59.         strWhere = " WHERE [Company] in (" & strIN & ") and [OCR_TEXT] Like '*" & strOCR & "*' AND [DOC_DATE]>=  " & strStartDate & " and [DOC_DATE] <= " & strEndDate & " "
  60. ElseIf ....
  61.     End If
  62.  
  63.     strSQL = strSQL & strWhere
  64.  
  65.  
  66.     db.QueryDefs.Delete "qryListSubForm"
  67.     Set qryDef = db.CreateQueryDef("qryListSubForm", strSQL)
  68.  
  69.     'Open the query, built using the IN clause to set the criteria
  70.     DoCmd.OpenForm "ListSubForm"
  71.  
  72.     'Clear listbox selection after running query
  73.     'Me.FilterOn = False
  74.     Me.lstCompany = ""
  75.     Me.txtAuthor = ""
  76.     Me.txtChemical = ""
  77.     Me.lstDocType = ""
  78.     Me.lstLanguage = ""
  79.     Me.txtStart = ""
  80.     Me.txtEnd = Date
  81.     Me.txtOCR = ""
  82.  
  83.  
  84. Exit_cmdSearch1_Click:
  85.     Exit Sub
  86.  
  87. Err_cmdSearch1_Click:
  88.  
  89.    If Err.Number = 5 Then
  90.         MsgBox "You must make a selection(s) from the list", , "Selection Required !"
  91.         Resume Exit_cmdSearch1_Click
  92.     Else
  93.     'Write out the error and exit the sub
  94.         MsgBox Err.Description
  95.         Resume Exit_cmdSearch1_Click
  96.     End If
  97. End Sub
  98.  
  99. Private Sub Form_Load()
  100. Me.txtEnd.Value = Date
  101. strEndDate = Format(CDate(Me.txtEnd.Value), "\#m/d/yyyy\#")
  102. End Sub
Mar 28 '07 #7
Rabbit
12,516 Expert Mod 8TB
Put a Me.Refresh right after you run the search.
Mar 28 '07 #8
Put a Me.Refresh right after you run the search.
I tried putting after the code, before the code every where but no same problem...
Mar 28 '07 #9
grabit
22
Hi all,

I have a database that I've created a search form. Everything works great the only problem is the first time when the form is opened the search doesn't work, but after that it works. I've looked over and over at the code, there is nothing to trigger this problem. Can you guys think of something that I'm missing.

Thanks

Wana
I dont know what your using php or asp but i have just built a search function in coldfusion and this is the WHERE statement from my query

WHERE topic LIKE '%#form.subject#%' OR topic LIKE '% #form.subject#_' OR topic LIKE '_#form.subject# %' OR topic LIKE '% _#form.subject#_ %'
OR username LIKE '#form.subject#'
OR posttext LIKE '%#form.subject#%' OR posttext LIKE '% #form.subject#_' OR posttext LIKE '_#form.subject# %' OR posttext LIKE '% _#form.subject#_ %'

Topic, username and posttext are the columns im searching:
'%#form.subject#%' looks for all strings entered
'% #form.subject#_' looks for words with a punctustion mark after them and a space in front of them
'_#form.subject# %' looks for the punct mark in front of the word with a space behind it
'% _#form.subject#_ %' looks for words with punct marks at either end.

then out put in your language as normal.

Need any more help giz a yell

cheers
Grabit
Mar 28 '07 #10
Rabbit
12,516 Expert Mod 8TB
I noticed you didn't answer my first question "So the first time they click the button it doesn't work but it works the second time they click the button?"
Mar 28 '07 #11
I noticed you didn't answer my first question "So the first time they click the button it doesn't work but it works the second time they click the button?"
yes,
When they click search button it opens an other form with the results. for the first time the results are not right it brings everything from the database, the second time the results are correct.

Thanks

Wana
Mar 28 '07 #12
Rabbit
12,516 Expert Mod 8TB
Is there any code in the ListSubForm?
Mar 29 '07 #13
Is there any code in the ListSubForm?
No I don't have any code in my Subform, it just displays the records from the query.

Thanks

Wana
Mar 29 '07 #14
Rabbit
12,516 Expert Mod 8TB
Hmm.. I'm pretty stumped. Before I toss this out to the other mods and see if they can figure this out, try moving the "Clear listbox selection after running query" stuff before the OpenForm.
Mar 30 '07 #15
Hmm.. I'm pretty stumped. Before I toss this out to the other mods and see if they can figure this out, try moving the "Clear listbox selection after running query" stuff before the OpenForm.
I don't have any answer to this, but yes this worked. As you said I moved the clear box selection to Form Open, and now it's working.

Thanks so much for your help.

Wana
Mar 30 '07 #16
Rabbit
12,516 Expert Mod 8TB
Not a problem. I've noticed that when you have code after you open a new object things tend to get a little screwy. So as a rule of thumb have the opening of an object be the last thing you do in a sub/function.
Mar 30 '07 #17
Not a problem. I've noticed that when you have code after you open a new object things tend to get a little screwy. So as a rule of thumb have the opening of an object be the last thing you do in a sub/function.
good idea thanks
Mar 31 '07 #18
Denburt
1,356 Expert 1GB
If you don't mind I just stumbled across this thread and wanted to point out a couple of things. I read the following two lines and it threw me. :)

Expand|Select|Wrap|Line Numbers
  1. db.QueryDefs.Delete "qryListSubForm"
  2.  Set qryDef = db.CreateQueryDef("qryListSubForm", strSQL)
  3.  
You should consider the following:
Expand|Select|Wrap|Line Numbers
  1. Set qryDef = db.QueryDefs("qryListSubForm")
  2. qryDef.SQL = strsql
  3. qryDef.close
  4. set qryDef = nothing
  5.  
You want to make sure you always close these connections or else you will leave the program consuming unnecessary resources, and when you close the DB you will notice a significant amount of ballooning.

Now on your issues with refreshing your control... If the following query "qryListSubForm" is the query that is in the subforms recordsource there is an occasion that you need to reset the recordsource of the subform.
However if it is work congrats if ya need anything else let us know.
me!Subform.form.recordsource = "qryListSubForm"
me!Subform.form.requery
Mar 31 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: WindAndWaves | last post by:
Hi Gurus I am keen to make a search page on a website, but I have absolutely zero experience with PHP. I am going to hire an expert, but I thought that it may pay to try it a bit first myself...
4
by: Captain Wonky | last post by:
As the subject says... I'm a database novice even though I've been trying to learn Access for years. I've 'almost finished' several databases but always get stumped on something - this time it's...
2
by: misschristalee | last post by:
I'm having a brain blockage day.... Scenario: Search Form with 6 text boxes Query has same six fields Each has this IIF: IIf(IsNull(!!),"",!!) with each dictating the correct text box of...
2
by: khan | last post by:
hi guys, I want to search query through a webpage. I want to set search criaria a web text filed same we do for forms {forms]!!. is it possible I could use a webpage text field as a search...
1
by: atl10spro | last post by:
Hello Everyone, I am new to MS Access and although I have created several different databases I lack the VB knowledge to code a search function. I am turning to your expertise for assistance. ...
1
by: Eddie Holder | last post by:
Hi Guys, I hope that someone will be able to help I have a table containing data, lets say products. I have a form with a text box which serves as the criteria for a query to search the product...
1
by: lushh | last post by:
hi.. i am currently creating an employee database.. and there is a form on the database that needs to search on records.. i am planning to create a form with a single textbox (for the keyword...
2
by: Robert Jacobs | last post by:
Please Help! I have read over and over again to go to Allen Browne's website to filter subforms, but I just don't understand it, and I can't get it to work in my situation. Any advice is very...
3
by: peterhall | last post by:
In VBA an Access module has a find method - works perfectly to find a string inside a module. i'm working in A97 (legacy) systems (large ones) and want to write code that searches all modules so that...
2
by: Mark | last post by:
Hi All, I am creating a music database for a friend and have run into a problem. Within the ALBUM table, I wanted to store the ARTIST_ID rather than the ARTIST_NAME. To do this, I intended to have...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.