UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Trying To Build A Query From Table, Office 2003    
 
   
AfroSheen15
post Jan 25 2011, 09:17 AM
Post #1

UtterAccess Addict
Posts: 283
From: Wilson, NC



Good morning.
I need to build a query from an inspection table based on the inspection date.

I'm using Allen Brown's search program and I'm trying to modify it so when I put in the dates it will build a query from the qryFireInspection based on the field called Inspdt.

If I'm using the same table then it will work on the dates. Because the qryFireInspection is based on a relationship between tblFire and tblFinspection {one to many} it doesn't appear to work.

This is the code I have so far:

CODE
If Not IsNull(Me.txtStartDate) Then
      StrWhere = StrWhere1 = "select inspdt from qryFireInspection where ([inspdt] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
   End If


The StrWhere is actually building a query string from all the information so when its finished, it will create a filter.

Its probably something simple that I've over looked.
Thanks for your help.
Go to the top of the page
 
+
Daryl S
post Jan 25 2011, 09:28 AM
Post #2

UtterAccess VIP
Posts: 2,270
From: Colorful Colorado



AfroSheen15 -

What type of format is conJetDate? If your dates are not stored as true dates, then you may need a text delimiter. And the date format would have to be YYYYMMDD or something that would sort properly for the >= operator to work.

If you are using true dates, then you don't need to format them. You are missing the date delimiters, so try this:

StrWhere = StrWhere1 = "select inspdt from qryFireInspection where ([inspdt] >= #" & Me.txtStartDate & "#) AND "

If that doesn't work, let us know what conJetDate is, and how your inspdt is stored...
- Daryl
Go to the top of the page
 
+
AfroSheen15
post Jan 25 2011, 09:45 AM
Post #3

UtterAccess Addict
Posts: 283
From: Wilson, NC



Thanks for the reply. Here is what conjet is

CODE
Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.
Go to the top of the page
 
+
Daryl S
post Jan 27 2011, 01:27 PM
Post #4

UtterAccess VIP
Posts: 2,270
From: Colorful Colorado



The problem is likely the two assignment statements - maybe this is a typo?

StrWhere = StrWhere1 = "select inspdt from qryFireInspection where ([inspdt] >= #" & Me.txtStartDate & "#) AND "

maybe should be this:

StrWhere = StrWhere1 & "select inspdt from qryFireInspection where ([inspdt] >= #" & Me.txtStartDate & "#) AND "

or this:

StrWhere = "select inspdt from qryFireInspection where ([inspdt] >= #" & Me.txtStartDate & "#) AND "

Try fixing that so you don't have two equal signs in the statement.

If you are getting an error message, please let us know what it is. Also, if you can add this code:
Debug.Print StrWhere
below the StrWhere = statement, then look in the immediate window to see how Access has built the strWhere, that should clue you in on any mistakes. You can put this statement in a few places as StrWhere is being built.

Hope that helps - if not, copy/paste the statement from the immediate window into your next post, and include any error messages you see.

- Daryl
Go to the top of the page
 
+
AfroSheen15
post Jan 28 2011, 07:48 AM
Post #5

UtterAccess Addict
Posts: 283
From: Wilson, NC



This is the complete build of the strWhere query.

CODE
? strwhere
select inspdt from qryFireInspection where ([inspdt] >= #1/1/2011#) AND select inspdt from qryFireInspection where ([inspdt] < #1/1/2011#) AND


It said that there was a syntax error in the query.
Go to the top of the page
 
+
AfroSheen15
post Jan 28 2011, 07:54 AM
Post #6

UtterAccess Addict
Posts: 283
From: Wilson, NC



Sorry, I had the code a little wrong.

The code is pasted as:

CODE
StrWhere1 = "select inspdt from qryFireInspection where ([inspdt] >= #" & Me.txtStartDate & "#) AND "
StrWhere = StrWhere & StrWhere1

The complet query string is:

? strwhere
select inspdt from qryFireInspection where ([inspdt] >= #1/1/2011#) AND select inspdt from qryFireInspection where ([inspdt] < #12/31/2011#) AND


I was getting a syntax query expression error
Go to the top of the page
 
+
Daryl S
post Jan 28 2011, 10:35 AM
Post #7

UtterAccess VIP
Posts: 2,270
From: Colorful Colorado



Afrosheen -

Normally we use strWhere to hold just a WHERE clause, and strSQL to hold a SQL statement. It looks like you are putting SQL statements into a WHERE clause. I suspect your strWhere should only look like this:
WHERE ([inspdt] >= #1/1/2011#) AND ([inspdt] < #12/31/2011#) AND

So I would update your StrWhere1 to this:
StrWhere1 = "([inspdt] >= #" & Me.txtStartDate & "#) AND "

Then I assume StrWhere is really a SQL statement. If it is only to hold the WHERE clause, then you need to do the same thing, and remove the text before the WHERE.

Then before you run this, you need to remove the last " AND " from the statement. These are usually added when we have a lot of optional criteria in code to build the WHERE clause. When all the criteria are added, the final " AND " needs to be removed. That statement could look like this:

StrWhere = Left(StrWhere,len(StrWhere)-5)
Print.Debug StrWhere

Give that a go.
- Daryl

Go to the top of the page
 
+
AfroSheen15
post Jan 28 2011, 10:52 AM
Post #8

UtterAccess Addict
Posts: 283
From: Wilson, NC



It started working until it came to the input box asking me for the field [inspdt]. That is from the table called tblFireInspection. The table the program opens with is called tblFire.

I may not be able to get the results I'm looking for. If that's the case then Ok, I'll deal with it..

Go to the top of the page
 
+
Daryl S
post Jan 28 2011, 11:53 AM
Post #9

UtterAccess VIP
Posts: 2,270
From: Colorful Colorado



Afrosheen -

Post your final full SQL statement (you can use a Debug.Print statement after all the code that builds the SQL). Maybe we can spot the issue.

- Daryl

Edit - I just noticed in my prior post I had Print.Debug instead of Debug.Print - don't know what my fingers were thinking!
Go to the top of the page
 
+
AfroSheen15
post Jan 28 2011, 12:09 PM
Post #10

UtterAccess Addict
Posts: 283
From: Wilson, NC



Sorry. It willl have to be next week. going out of town this weekend.

Will check back on Monday or Tuesday.
Go to the top of the page
 
+
AfroSheen15
post Feb 2 2011, 10:42 AM
Post #11

UtterAccess Addict
Posts: 283
From: Wilson, NC



Sorry for getting back so late.
Instead of trying to display information from one table to the next, what are your thoughts of doing a search on the product then display the inspection information in a combo box.

For example: Fire ext. "Baker" has 5 inspection records. On that same line if I click on the combo box then it will show all the information pertaining to Baker.

I don't know if it can be done or not.
Go to the top of the page
 
+
Daryl S
post Feb 2 2011, 11:09 AM
Post #12

UtterAccess VIP
Posts: 2,270
From: Colorful Colorado



Normally if you have a one-to-many relationship, you select the record from the 'one' side (e.g. Baker), and then display the records from the 'many' side in a subform, so that you can see all 5 inspection records.

Does that help?
- Daryl
Go to the top of the page
 
+
AfroSheen15
post Feb 2 2011, 11:25 AM
Post #13

UtterAccess Addict
Posts: 283
From: Wilson, NC



I believe it does. I already have a inspection subform created so I guess I could click on the fire ext. name and have it open the inspection history subform

Thanks for the help and direction.

Go to the top of the page
 
+
Daryl S
post Feb 2 2011, 11:53 AM
Post #14

UtterAccess VIP
Posts: 2,270
From: Colorful Colorado



Always happy to help!
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 18th May 2013 - 05:52 PM