Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I reference a field in query as variable

Status
Not open for further replies.

craigglenn

Technical User
Oct 30, 2003
42
US
Ok, I have searched and just can't figure this out. The problem is that I need to order a report on a field that is determined by user imput for a certain day they chose. The basics...
Property Route report based on seven days
Get the day user want to print the report for
get the route number to report on
since properties are sweep on different nights and my be on different routes i had to have fields for the route number and priority for each property for that route.

for example property abc may be sweep on M,W,F
on Monday it is on Route 1
On wednesday it is on Route 2
then Friday back on route 1

the key is to sort each route with a priorty field supplied based on day and route. Her is my code. How do I reference the priority field in the query?

Thanks in advance.

Private Sub Report_NoData(Cancel As Integer)

MsgBox ("No records found for specified route.")
Cancel = True

End Sub

Private Sub Report_Open(Cancel As Integer)

Dim routeN As Integer
Dim rDay As Integer
Dim theQuery As String
Dim dayField As String
Dim priority As String

GetRouteDay rDay 'prompt for route day
GetRouteNum routeN 'prompt for route number

Select Case rDay
Case 1
dayField = "PropSwpMonday"
priority = PropPriorityMonday
Case 2
dayField = "PropSwpTuesday"
priority = [PropPriorityTuesday]
Case 3
dayField = "PropSwpWednesday"
priority = PropPriorityWednesday
Case 4
dayField = "PropSwpThursday"
priority = PropPriorityThursday
Case 5
dayField = "PropSwpFriday"
priority = PropPriorityFriday
Case 6
dayField = "PropSwpSaturday"
priority = PropPrioritySaturday
Case 7
dayField = "PropSwpSunday"
priority = PropPrioritySunday
End Select

theQuery = "Select * From mainProperty Where " & dayField & "=" & routeN & " Order By " & priority & ";"
Me.RecordSource = theQuery

End Sub

Private Function GetRouteNum(routeN) As Integer

On Error GoTo ErrorHandler
Dim message, title
message = "Enter Route Number:"
title = "Route Number"
routeN = InputBox(message, title)

Exit Function
ErrorHandler:
Select Case Err.Number
Case 13
MsgBox ("You must enter a valid route number")
Case Else
MsgBox ("You must enter a valid route number")
End Select
Resume


End Function


Private Function GetRouteDay(rDay) As Integer

On Error GoTo ErrorHandler
Dim message, title
message = "Enter number for day of route:" + (Chr(13)) + "1. Monday" + (Chr(13)) + "2. Tuesday" + (Chr(13)) + "3. Wednesday" + (Chr(13)) + "4. Thursday" + (Chr(13)) + "5. Friday" + (Chr(13)) + "6. Saturday" + (Chr(13)) + "7. Sunday"
title = "Route day"

InvalidDay:

rDay = InputBox(message, title)

Select Case rDay
Case "1", "2", "3", "4", "5", "6", "7"
Exit Function
Case Else
MsgBox ("You must enter a valid Day Number")
GoTo InvalidDay
End Select

ErrorHandler:
Select Case Err.Number
Case 13
MsgBox ("You must enter a valid day")
Case Else
MsgBox ("You must enter a valid day")
End Select
Resume

End Function
 
Hi

To begin, teh order of a report is not based on the order of the underlying query, it is based on the .OrderBy property of the report

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Hi

See thread703-699020 for how to change teh report sort order at run time, using variables from a form

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Thanks for the responce Ken, Now let me see if I understand. I see that it sould not be done from the sql statement. I should use the orderby property. Now, should these statements be included in my select case statements (where the one's that don't work are) or after the query?

Thanks,
Craig
 
This is what I tried without success on my part I know. Thanks in advance for your help.

Select Case rDay
Case 1
dayField = "PropSwpMonday"
Me.OrderBy = "[PropPriorityMonday]"
Case 2
dayField = "PropSwpTuesday"
Me.OrderBy = "[PropPriorityTuesday]"
Case 3
dayField = "PropSwpWednesday"
Me.OrderBy = "[PropPriorityWednesday]"
Case 4
dayField = "PropSwpThursday"
Me.OrderBy = "[PropPriorityThursday]"
Case 5
dayField = "PropSwpFriday"
Me.OrderBy = "[PropPriorityFriday]"
Case 6
dayField = "PropSwpSaturday"
Me.OrderBy = "[PropPrioritySaturday]"
Case 7
dayField = "PropSwpSunday"
Me.OrderBy = "[PropPrioritySunday]"
End Select

theQuery = "Select * From mainProperty Where " & dayField & "=" & routeN & ";"
Me.RecordSource = theQuery
 
Hi

No, you need to set the .OrderBy property in the on open event of the form, if you look at the thread I gave you it is all there, I answered a similar question yesterday or the day before

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Thanks Ken,

Sorry for being a pain. I am using the on open event for the report. All of the code I should you is from that event. I will read the thread again and see if I missed something.

Thanks,
Craig
 
Ken,

Yea, I caught that after the last post when I reread the thread. Here's what I have now and still no luck.

Private Sub Report_Open(Cancel As Integer)

Dim routeN As Integer
Dim rDay As Integer
Dim theQuery As String
Dim dayField As String

GetRouteDay rDay 'prompt for route day
GetRouteNum routeN 'prompt for route number

Select Case rDay
Case 1
dayField = "PropSwpMonday"
Me.OrderBy = "[PropPriorityMonday]"
Case 2
dayField = "PropSwpTuesday"
Me.OrderBy = "[PropPriorityTuesday]"
Case 3
dayField = "PropSwpWednesday"
Me.OrderBy = "[PropPriorityWednesday]"
Case 4
dayField = "PropSwpThursday"
Me.OrderBy = "[PropPriorityThursday]"
Case 5
dayField = "PropSwpFriday"
Me.OrderBy = "[PropPriorityFriday]"
Case 6
dayField = "PropSwpSaturday"
Me.OrderBy = "[PropPrioritySaturday]"
Case 7
dayField = "PropSwpSunday"
Me.OrderBy = "[PropPrioritySunday]"
End Select
Me.OrderByOn = True

theQuery = "Select * From mainProperty Where " & dayField & "=" & routeN & ";"
Me.RecordSource = theQuery

End Sub


Thanks again,
Craig
 
Ken,

No I did not see reference to that in the thread. However, that was my problem. Thanks so much for your help. Well the sun just came up here on the east coast of the US, so I think I will go to bed.

Thanks again,
Craig Glenn
Orlando, FL USA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top