Experts help! Search form, but tougher!

dillonhh

Registered User.
Local time
Today, 04:57
Joined
Nov 2, 2007
Messages
38
Hello all,

So, with the help of some other threads I have successfully created a few search forms. I will focus on just one here even though I will do this for a few different searches.

Right now I have a form that has some different unbound text boxes that the user can fill in to search records...ReservationIDSearchText to search a ReservationID from a specifies query as well as the salesperson, arrival date range, return date range, etc. The code creates and opens a dynamic query which all works very nicely. However, I am looking to do something more advanced.

Here is what I have so far:

Code:
Option Compare Database

Private Sub cmdReservations_Search_Click()
Dim db As Database
Dim QD As QueryDef
Dim where As Variant

Set db = CurrentDb()

' Delete the existing dynamic query; trap the error if the query does
' not exist.
On Error Resume Next
db.QueryDefs.Delete ("dynqryReservations_SearchResults")
On Error GoTo 0

where = Null
where = where & " AND [ReservationID]= " + Me![ReservationIDSearchText]
where = where & " AND [EmployeeID]= " + Me![EmployeeIDSearchText]
where = where & " AND [ReservationStatusID]= " + Me![ReservationStatusIDSearchText]

' The following evaluates the date search information.

If Not IsNull(Me![ArrivalEndDateSearchText]) Then
    where = where & " AND [ArrivalDate] between #" + _
    Me![ArrivalStartDateSearchText] + "# AND #" & Me![ArrivalEndDateSearchText] & "#"
Else
    where = where & " AND [ArrivalDate] >= #" + Me![ArrivalStartDateSearchText] _
    + " #"
End If

If Not IsNull(Me![ReturnEndDateSearchText]) Then
    where = where & " AND [ReturnDate] between #" + _
    Me![ReturnStartDateSearchText] + "# AND #" & Me![ReturnEndDateSearchText] & "#"
Else
    where = where & " AND [ReturnDate] >= #" + Me![ReturnStartDateSearchText] _
    + " #"
End If

' Remove the following MsgBox line if you do not want to display the
' SQL statement.

'MsgBox "Select * from qryReservations_Search " & (" where " + Mid(where, 6) & ";") 'No need for now
Set QD = db.CreateQueryDef("dynqryReservations_SearchResults", _
"Select * from qryReservations_Search " & (" where " + Mid(where, 6) & ";"))
DoCmd.OpenQuery "dynqryReservations_SearchResults"
End Sub

So here are the things I would like to change but do not know how:

1. If I change the text box properties on the search form, so that the ReservationIDSearchText is a number and the date fields are formatted a certain way the above code returns errors. I do not want people to enter letters stupidly for ReservationID or the dates in the range, so is there another way to validate this that will not cause errors?

2. Here is what I call the hard part: Instead of opening a dynamic query, I would like the button click to open some kind of popup with the same datasheet type view but that has the sole purpose of selecting the record the user has searched from the results and then opening it in the regular form for editing the reservations which already exists...say frmReservations. The datasheet style popup would not allow users to edit anything at all, just a list in essence, to select from the search results and open the record in the normal editing form. I hope I am explaining this clearly.

So, an example, I enter a date range for a certain client(s) arrival and the list popups with the search results in a datasheet form(maybe a dozen records or whatever), ReservationID,ArrivalDate,ReturnDate,EmployeeID, etc. (these fields are whatever exists in the source query). Then the user can select which Reservation they are looking for in this list, the list closes, and the record pops up in its edit form that we use to enter reservations to begin with.

I think this is a good start, let me know what i can clear up, and thanks!
 
So here are the things I would like to change but do not know how:

1. If I change the text box properties on the search form, so that the ReservationIDSearchText is a number and the date fields are formatted a certain way the above code returns errors. I do not want people to enter letters stupidly for ReservationID or the dates in the range, so is there another way to validate this that will not cause errors?
I suppose you could write conditionals for the controls based on the formats of the values entered. Say like, If Format(), Then... MgsBox? Control.SetFocus?
2. Here is what I call the hard part: Instead of opening a dynamic query, I would like the button click to open some kind of popup with the same datasheet type view but that has the sole purpose of selecting the record the user has searched from the results and then opening it in the regular form for editing the reservations which already exists...say frmReservations.
How about just a DoCmd.OpenQuery "acformDS", "acReadOnly" for this?
The datasheet style popup would not allow users to edit anything at all, just a list in essence, to select from the search results and open the record in the normal editing form.
I have seen quite a few questions about this, and I think I told someone once to use the double click event of the datasheet view's field of choice. That way, you can open a new form that just displays the current record of the datasheet that was doublclicked. (DoCmd.OpenForm "WHERE clause here using "Forms!, etc..)

But, if you want the list to close, just add that command after the OpenForm command. Does this get you started? Also, I was wondering how long you have been working with Visual Basic, and/or SQL? Do you think it's possible to take what you've written in the module and put it into a built-in query object, or two?
 
Last edited:
Hey Adam, thanks a lot for responding,

I actually have a lot less experience with VBA/SQL than that code makes it seem. I have a few years experience with other languages so I have been picking it up pretty quick, but ya pretty green still. I have been using a lot of outside sources to get what I need.

Anyway, I was trying to do just that with the DoCmd.OpenQuery acViewNormal, acReadOnly. The tough part comes with setting that doubleclick event since the code deleted the query every time it is run. Wait, its a query, so I can't even have a doubleclick event right? But if I could just figure that out, it would be perfect.

I did not yet go back about the first question I had about validation...

Thanks again, Dillon
 
The tough part comes with setting that doubleclick event since the code deleted the query every time it is run. Wait, its a query, so I can't even have a doubleclick event right?
That is right. That is impractical anyway, and it's not what queries are used for. I would simply create an autoform based on this query you created, and let it sit there with a recordsource of that query. It doesn't matter if the query is present in the Definitions collection or not, that doesn't affect the source of the form (if you try to open the form with no query def to call, it will just throw an error at you). Once your query is opened, open the form with a default view of datasheet, and use the double click strategy with it. That's what I was meaning to get at...
 
Hey Adam, that is a great idea, I wish I thought of it myself...

So I have gotten it to work well except for one little case that I thought maybe you could help me with.

When I am writing the WHERE conditions, normally the form I am opening has that same field in the parent form. For example, I search Customers, the form for search results pops up, double click a customer and a form comes up with Customers that has a subform with that customer's Reservations.

But now I want to build the same search for reservations, the results pop up, and then I want to double click a reservation to open the same Customers form with the Reservation as a subform still with the ability to see that Reservation's Customer in the parent part of the form. The problem is, how to code the DoCmd to open the form but have the WHERE condition compare the search results form with a subform?

Example,
Code:
DoCmd.OpenForm "frmCustomersReservations", , , "??LOST_HERE???[ReservationID] = [Forms]![dynfrmReservations_SearchResults]![ReservationID]"

Hope that is clear, it is a tough one to explain succinctly. Thanks,

D
 
Dillon,

It sounds like you pretty much have one form that holds everything, correct? Like customer, reservations, etc... It sounds like a typical one-to-many relationship to. If this is the case, the corresponding tables are obviously joined somehow, so you will have to lookup a value I think. What I'm thinking is this...

1) Open the form with the code you have now, but modify it a bit...
Code:
docmd.openform "frmcustomersreservations", , , 
   
    "[customer] = DLookup('[customer]', 'customerstable', 

'[FieldLinkedToTheReservationsTable] = Forms!DSformThatIsOpen!FieldYouClickedOn')"
2) Then, maybe a "Load" event for the form that holds the displayed end result? No need to do this if you don't want to see a filtered reservation record, but if you do, how about...
Code:
OnLoad

  me.subcontainername.form!.filter = [reservationfield] = 
    forms!DSform!reservationfield

  me.subcontainername.form!.filteron = true
 
Hey, thanks a lot for the help, I will keep cracking at it. I really appreciate it. -D
 
Hi Adam, so if I do that load event for the subform, it will run no matter how I open that form. Sometimes that form will be opened with no filters through the switchboard and start at the beginning. However, if I write in that load event, the filter will filter the reservations even if I am not opening through that search results datasheet. Am i correct?

Thanks, D
 
Forgive me, but i am having a problem with the first part too...

The Dlookup turns out the correct customer number, but I cannot write it into the entire statement like this:

Code:
DoCmd.OpenForm "frmCustomersReservations", , , "[CustomerID]=DLookup("CustomerID", "Customers", "ReservationID=" & Forms!dynfrmReservations_SearchResults!ReservationID)"

or else it gives me an error, highlighting the "CustomerID" at the beginning of the lookup and saying it expects the end of the expression.

But i know the lookup is right because i tested it on its own with MsgBox. I tried to set the lookup equal to an integer S, but i am having trouble there too...

Code:
Dim S As Integer
S = DLookup("CustomerID", "Customers", "ReservationID=" & Forms!dynfrmReservations_SearchResults!ReservationID)
DoCmd.OpenForm "frmCustomersReservations", , , "[CustomerID]=S"

When i run this, a parameter box pops up asking me for the value of S. the CustomerID is simply a 7 digit number, am i initializing the variable wrong?

thanks, d
 
Hi Adam, so if I do that load event for the subform, it will run no matter how I open that form.
No, not unless you go through the process of the search datasheet. That's what I wrote the code to comply with. To change it, we might have to go through this again! :)
However, if I write in that load event, the filter will filter the reservations even if I am not opening through that search results datasheet. Am i correct?
Nope, and it's because of the criteria line...
Code:
me.subcontainername.form!.filter = [B][U][reservationfield] = 
    forms!DSform!reservationfield[/U][/B]
 
Correct this. Change the following...
Code:
DoCmd.OpenForm "frmCustomersReservations", , , 
   "[CustomerID]=DLookup("CustomerID", "Customers", 
      "ReservationID=" & Forms!dynfrmReservations_SearchResults!ReservationID)"
to this...
Code:
DLookup("[CustomerID]", "Customers", 
   "ReservationID = Forms!dynfrmReservations_SearchResults!ReservationID)"
I tried to set the lookup equal to an integer S, but i am having trouble there too...

Code:
Dim S As Integer
S = DLookup("CustomerID", "Customers", "ReservationID=" & Forms!dynfrmReservations_SearchResults!ReservationID)
DoCmd.OpenForm "frmCustomersReservations", , , "[CustomerID]=S"
Same problem as above. Try the above correction without messing around with an integer variable. I would bank on the fact that quotation marks and brackets are again troubling people using functions (happens all too frequently!!)
 
the corrected code you posted is giving me syntax error and will not compile
 
Here is the latest code for one of these searches (no dates on this one!)

Code:
Option Compare Database

Private Sub cmdEmployees_Search_Click()
Dim db As Database
Dim QD As QueryDef
Dim where As Variant

Set db = CurrentDb()

' Delete the existing dynamic query; trap the error if the query does
' not exist.
On Error Resume Next
db.QueryDefs.Delete ("dynqryEmployees_SearchResults")
DoCmd.Close acForm, "dynfrmEmployees_SearchResults", acSaveNo
DoCmd.Minimize
On Error GoTo 0

' Note that there are no type-casting characters surrounding the
' numeric field [CustomerID].

where = Null
where = where & " AND [EmployeeID]= " + Me![EmployeeIDSearchText]
where = where & " AND [DepartmentID]= " + Me![DepartmentIDSearchText]

' Note the single quotation marks surrounding the following text fields.

' The following section evaluates the criteria you enter.
' If the first or last character of the criteria is the wildcard
' character (*), then the function uses the "LIKE" operator in the
' SQL statement instead of "=".

If Left(Me![FirstLastNameSearchText], 1) = "*" Or Right(Me![FirstLastNameSearchText], 1) = "*" Then
   where = where & " AND [FirstLastName] like '" + Me![FirstLastNameSearchText] + "'"
Else
   where = where & " AND [FirstLastName] = '" + Me![FirstLastNameSearchText] + "'"
End If


If Left(Me![FirstNameSearchText], 1) = "*" Or Right(Me![FirstNameSearchText], 1) = "*" Then
   where = where & " AND [FirstName] like '" + Me![FirstNameSearchText] + "'"
Else
   where = where & " AND [FirstName] = '" + Me![FirstNameSearchText] + "'"
End If


Set QD = db.CreateQueryDef("dynqryEmployees_SearchResults", _
"Select * from qryEmployees_Search " & (" where " + Mid(where, 6) & ";"))
DoCmd.OpenForm "dynfrmEmployees_SearchResults", acFormDS
End Sub

Everything works perfectly well with Access but, I am trying to upsize this database to SQL Server, and this very last line is now giving me an error "Object variable, or With block variable not set". I should not necessarily blame it on this line, but that is the line that ends up highlighted in VB debug mode.

Code:
Set QD = db.CreateQueryDef("dynqryEmployees_SearchResults", _
"Select * from qryEmployees_Search " & (" where " + Mid(where, 6) & ";"))


Does anyone have any idea why that may be?
Thanks a lot, D

PS And yes i know, i should change the + to &...i keep forgetting. But i just tried that after pasting this code and it at least does not seem to be the problem...
 
Last edited:
The code, which you may remember well, is just taking in any information that the user enters in a search form, such as last name, first name, etc. As long as the user has added something to the search form, a statement that is called "where" is created with each criteria.

The last part of the code uses this "where" to search the records. Therefore, if the search boxes are left empty then every record shows up in the results.

This is where I originally stole the code from: http://support.microsoft.com/kb/210242/en-us

I have made some small changes but that is the gist of it. My problem now is just the error message when I am using the Upsized version of this database with SQL Server as the back end and Access as the front end. Everything upsizes without any problem, there were a few functions i had to change because of formatting differences, such as with dates, but this problem in the post above is one i have not been able to find a fix for.

I read that the error i explained above comes up as an SQL Server 2005 glitch sometimes, but I really cannot say that this example is part of the glitch that microsoft outlines online.

d
 

Users who are viewing this thread

Back
Top Bottom