473,397 Members | 2,099 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,397 software developers and data experts.

How can open a recordset with conditions

106 100+
Hi,

I am opening recordset that performs well until I set some criteria in the underlying query.

This is my query and VBA code that work:
Expand|Select|Wrap|Line Numbers
  1. SELECT Booking.BookingID, Booking.BookingDate, Booking.BookingTime, Booking.EstimateTime, ACC_Vehicle.Reg, Staff.StaffName, Booking.MechanicID
  2. FROM (Booking INNER JOIN ACC_Vehicle ON Booking.ACCVehicleID = ACC_Vehicle.ACCVehicleID) INNER JOIN Staff ON Booking.MechanicID = Staff.StaffID;
  3.  
Expand|Select|Wrap|Line Numbers
  1.     'set array
  2.     Dim TimeSlot(9, 3) As Long 'Define Array
  3.     Dim TimeReg(9) As String ' array for vehicle reg
  4.     Dim NoofBookings As Integer, A As Integer 'Set Counters
  5.     Dim lngHours As Long, lngMins As Long 'Seperate Hours Mins of Booking Time
  6.     Dim lngDurHours As Long, lngDurMins As Long ' Seperate Hours and Mins of Duration
  7.  
  8.     Dim LeftPos As Long, tw As Long  'Define StartPosition for Chart
  9.  
  10.     'Configure Datbase and RecordSet
  11.     Dim rs As Recordset
  12.     Dim db As Database
  13.  
  14.     Set db = CurrentDb
  15.     Set rs = db.OpenRecordset("Booking_TimeGraph", dbOpenDynaset)
  16.  
  17.     'Count no. of bookings
  18.     rs.MoveFirst
  19.     rs.MoveLast
  20.     NoofBookings = rs.RecordCount
The moment I change my query to include a filter, the form gives an error message "Too Few Parameters expected 1". (Query opens to give required results). VBA code is in the ON-OPEN property of the form.
I feel that critera has to be set when opening the recordset rather than in the underlying query, but I'm not sure how to do that. :)

Expand|Select|Wrap|Line Numbers
  1. SELECT Booking.BookingID, Booking.BookingDate, Booking.BookingTime, Booking.EstimateTime, ACC_Vehicle.Reg, Staff.StaffName, Booking.MechanicID
  2. FROM (Booking INNER JOIN ACC_Vehicle ON Booking.ACCVehicleID = ACC_Vehicle.ACCVehicleID) INNER JOIN Staff ON Booking.MechanicID = Staff.StaffID
  3. WHERE (((Booking.MechanicID)=[Forms]![Bookings]![MechanicID]));
  4.  
Aug 28 '10 #1
9 3476
ADezii
8,834 Expert 8TB
I feel that critera has to be set when opening the recordset rather than in the underlying query
Not true, kindly display all Code in the Open() Event.
Aug 28 '10 #2
tasawer
106 100+
* UPDATE - PLEASE DO NOT WORK IN THIS YET AS I AM MAKING SEVERAL CHANGES TO MY BOOKINGS FORM *

Thank you providing a solution to my previous problem. initially I was using the boxes.. but I am now using a label (lbl) becuase I need to display vehicle reg as the caption.

the object of this code is to convert the booking time and job duration to twips and postion a label in an appropriate position to represent used time slots. The label will display Vehicle Reg.

As for query problem. Error is generated on two seperate conditions.
1. When MechanicID exists but Booking time is Null - I get the error, 'The Control or Subform Control is too large for this Location.

2. Whenever the criteria is set to Forms!Bookings!MechanicID, I get error 3061 Too Few Parameters

Complete code is provided here.
Expand|Select|Wrap|Line Numbers
  1. On Error GoTo Error_TimeSlots
  2.  
  3.     Me.Caption = "Bookings for " & Me.StaffName
  4.  
  5.     'set array
  6.     Dim TimeSlot(13, 3) As Long 'Define Array
  7.     Dim TimeReg(13) As String ' array for vehicle reg
  8.     Dim NoofBookings As Integer, A As Integer, wot As Integer 'Set Counters
  9.     Dim lblx As Label 'to reference multiple labels on form
  10.  
  11.     Dim LeftPos As Long, tw As Long  'Define StartPosition for Chart
  12.  
  13.     'Configure Datbase and RecordSet
  14.     Dim rs As Recordset
  15.     Dim db As Database
  16.  
  17.     Set db = CurrentDb
  18.     Set rs = db.OpenRecordset("Booking_TimeGraph", dbOpenDynaset)
  19.  
  20.     'Count no. of bookings
  21.     rs.MoveFirst
  22.     rs.MoveLast
  23.     NoofBookings = rs.RecordCount
  24.  
  25. '    MsgBox NoofBookings
  26.  
  27.     rs.MoveFirst
  28.     For A = 1 To NoofBookings
  29.  
  30.             'Seperate Hours and Minutes
  31.             TimeSlot(A, 0) = IIf(IsNull(rs!BookingTime), 0, Format(rs!BookingTime, "h"))
  32.             TimeSlot(A, 1) = IIf(IsNull(rs!BookingTime), 0, Format(rs!BookingTime, "n"))
  33.             TimeSlot(A, 2) = IIf(IsNull(rs!EstimateTime), 0, Format(rs!EstimateTime, "h"))
  34.             'When Duration time is not set a thin line will indicate job start time
  35.             TimeSlot(A, 3) = IIf(IsNull(rs!EstimateTime), 5, Format(rs!EstimateTime, "n"))
  36.             TimeReg(A) = rs!Reg
  37.  
  38.             rs.MoveNext
  39.  
  40.         Next A
  41.  
  42. 'loop to draw boxes
  43.  
  44.     wot = 9 'Workshop Opening Time
  45.     LeftPos = 1 'start pos of 1 cm
  46.     tw = 567 '567 twips per cm
  47.  
  48.     'Hide all Labels
  49.     For A = 1 To 13
  50.         Set lblx = Me.Controls("lbl" & CStr(A))
  51.         lblx.Visible = False
  52.         Next A
  53.  
  54.     For A = 1 To NoofBookings
  55.  
  56.         Set lblx = Me.Controls("lbl" & CStr(A))
  57.             lblx.Caption = TimeReg(A)
  58.             lblx.Visible = True
  59.             lblx.Left = ((((TimeSlot(A, 0) + (TimeSlot(A, 1) / 60) - wot) * 2) + LeftPos)) * tw
  60.             lblx.Width = ((TimeSlot(A, 2) + (TimeSlot(A, 3) / 60))) * tw * 2 'x2 becuase 2cm represent 1 hour
  61.             lblx.Top = tw
  62.             lblx.Caption = TimeReg(A)
  63.  
  64.         Next A
  65.  
  66. Error_Exit: Exit Sub
  67. Error_TimeSlots: MsgBox Err.Number & " " & Err.Description, , "Time Slots"
  68.     Resume Error_Exit
  69.  
  70.  
Aug 28 '10 #3
tasawer
106 100+
HI Adezii,

I have finished my mods on the booking form.

Everytime I put criteria in the underlying query, I get the error 3061 Too Few Parameters

This query works:
Expand|Select|Wrap|Line Numbers
  1. SELECT Booking.BookingID, Booking.BookingDate, Booking.BookingTime, ACC_Vehicle.Reg, Staff.StaffName, Booking.MechanicID, Booking.JobEndTime, Booking.JobEndDate, Booking.bkTimeHours, Booking.bkTimeMins, Booking.BookingStatusID
  2. FROM (Booking INNER JOIN ACC_Vehicle ON Booking.ACCVehicleID = ACC_Vehicle.ACCVehicleID) INNER JOIN Staff ON Booking.MechanicID = Staff.StaffID
  3. WHERE (((Booking.BookingTime) Is Not Null) AND ((Booking.BookingStatusID)<>5));
  4.  
This Query will give errors when the associated form is opened. Otherwise it will give desired results.
Expand|Select|Wrap|Line Numbers
  1. SELECT Booking.BookingID, Booking.BookingDate, Booking.BookingTime, ACC_Vehicle.Reg, Staff.StaffName, Booking.MechanicID, Booking.JobEndTime, Booking.JobEndDate, Booking.bkTimeHours, Booking.bkTimeMins, Booking.BookingStatusID
  2. FROM (Booking INNER JOIN ACC_Vehicle ON Booking.ACCVehicleID = ACC_Vehicle.ACCVehicleID) INNER JOIN Staff ON Booking.MechanicID = Staff.StaffID
  3. WHERE (((Booking.BookingDate)=[Forms]![Bookings]![BookingDate]) AND ((Booking.BookingTime) Is Not Null) AND ((Booking.BookingStatusID)<>5));
  4.  
This is my code in the ON_Open() event:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. On Error GoTo Error_TimeSlots
  3.  
  4.     Me.Caption = "Bookings for " & Me.StaffName
  5.  
  6.     'Define array
  7.     Dim lngTimeSlot(14, 3) As Long 'Define Array
  8.     Dim strTimeReg(14) As String ' array for vehicle reg
  9.     Dim intNoofBookings As Integer, intCounter As Integer, wot As Integer 'Set Counters
  10.     Dim lblx As Label 'to reference multiple labels on form
  11.  
  12.     Dim LeftPos As Long, tw As Long  'Define StartPosition for Chart
  13.  
  14.     'Configure Datbase and RecordSet
  15.     Dim rs As Recordset
  16.     Dim db As Database
  17.  
  18.     Set db = CurrentDb
  19.     Set rs = db.OpenRecordset("Booking_TimeGraph", dbOpenDynaset)
  20.  
  21.     'Count no. of bookings
  22.     rs.MoveFirst
  23.     rs.MoveLast
  24.     intNoofBookings = rs.RecordCount
  25.  
  26.     'MsgBox intNoofBookings
  27.  
  28.     rs.MoveFirst
  29.     For intCounter = 1 To intNoofBookings
  30.  
  31.             'Seperate Hours and Minutes
  32.             lngTimeSlot(intCounter, 0) = IIf(IsNull(rs!BookingTime), 0, Format(rs!BookingTime, "h"))
  33.             lngTimeSlot(intCounter, 1) = IIf(IsNull(rs!BookingTime), 0, Format(rs!BookingTime, "n"))
  34.              'Calculate Time Difference between Job Start Time and Job End Time (in Minutes)
  35.             lngTimeSlot(intCounter, 2) = DateDiff("n", rs!BookingTime, rs!JobEndTime)
  36.             strTimeReg(intCounter) = rs!Reg
  37.  
  38.             rs.MoveNext
  39.  
  40.         Next intCounter
  41.  
  42. 'loop to draw boxes
  43.  
  44.     wot = 9 'Workshop Opening Time
  45.     LeftPos = 1 'start pos of 1 cm
  46.     tw = 567 '567 twips per cm
  47.  
  48.     'Hide all Labels
  49.     For intCounter = 1 To 14
  50.         Set lblx = Me.Controls("lbl" & CStr(intCounter))
  51.         lblx.Visible = False
  52.         Next intCounter
  53.  
  54.     For intCounter = 1 To intNoofBookings
  55.  
  56.         Set lblx = Me.Controls("lbl" & CStr(intCounter))
  57.             lblx.Visible = True
  58.             lblx.Left = ((((lngTimeSlot(intCounter, 0) + (lngTimeSlot(intCounter, 1) / 60) - wot) * 2) + LeftPos)) * tw
  59.             lblx.Width = ((lngTimeSlot(intCounter, 2) / 60)) * tw * 2 'x2 becuase 2cm represent 1 hour
  60.             lblx.Top = tw
  61.             lblx.Caption = strTimeReg(intCounter)
  62.  
  63.         Next intCounter
  64.  
  65. Error_Exit: Exit Sub
  66. Error_TimeSlots: MsgBox Err.Number & " " & Err.Description, , "Time Slots"
  67.     Resume Error_Exit
  68.  
  69. End Sub
Sep 1 '10 #4
ADezii
8,834 Expert 8TB
I would actually have to see the Database at this point, any chance of Uploading it?
Sep 1 '10 #5
tasawer
106 100+
can I send it to your personal email as I would not like anyone to open it? and How?
Sep 2 '10 #6
tasawer
106 100+
Hi Adezii,

I have found the answer.

My original code was for a module where I was opening a recordset
when I moved the code to a form that had an underlying query, it was opening recordset on info that was already there.

I have done a RecordClone and it works fine.

Regards
Sep 2 '10 #7
ADezii
8,834 Expert 8TB
Glad you found the answer, and shared it with us.
Sep 2 '10 #8
tasawer
106 100+
Hi Adezii,
Further to the above, I am now stuck on the logic to display the jobs as a pictograph.

it works this way:

1) A booking is made for a certain date
2) Booking time is selected
3) Mechanic Name is selected
3) Approximate time on job is specified.
4) Logic works out the JobEndDate & Time.

Click of a command button displays this booking as a chart together with other bookings for the day.

if a job is to last over several days, this job has to be seen on the pictograph whenever the correspnding date is selected. At the moment I am not able to achieve this.

can you look at this for me please?
I can email my database to your personal email address.
Sep 2 '10 #9
ADezii
8,834 Expert 8TB
I've given you my E-Mail Address in a Private Message. Please provide as much detail as possible, including Object Names, Events, Error Messages, etc., and I'll look at it as soon as I can.
Sep 2 '10 #10

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

Similar topics

1
by: kgs | last post by:
Problem exists on ms-access, ms-sqlserver using ADO (not in ODBC), visual Basic, C#, and VB.NET. Somethimes when I open second recordset in this same connection that first, it has EOF, but I know,...
4
by: Dalan | last post by:
I presume that using an open recordset method is the preferred method of accomplishing what I'm trying to do. Of course, if there are other options that would work, feel free to share them. I...
2
by: Colleyville Alan | last post by:
I ran into problems in an app in which I received a msg of "cannot lock the table, it is in use by another user". It turns out that I had opened a recordset with a command like set rstmyrecs =...
2
by: Sunil Korah | last post by:
I am having some trouble with opening recordsets. I have used code more or less straight from the access help. But still I am getting some errors. I am unable to work out what exactly I am doing...
12
by: (Pete Cresswell) | last post by:
Say I've got a RecordSet: ----------------------------------------------------------- Set myRS = CurrentDB.OpenRecordset(SomeSQL, dbOpenDynaset)...
0
by: solar | last post by:
I am creating a new recordset in 2 tables, orders and order details.Obviously i have to open the recordset for two tables.Shall i open the recordset for the table order details and when shall i...
2
by: kifaro | last post by:
Hi I am opening recordsets in my asp page with the following command: rs.open mysql,cn,3,3 on occasion it isn't working and I have to do: set rs=cn.execute(mysql) Any clue why?? Regards,...
2
by: Kosmos | last post by:
I am opening the following connections as such although I am only referring to the first connection for this question: Dim con1 As ADODB.Connection Dim con2 As ADODB.Connection Dim recSet1 As...
2
by: banderson | last post by:
Hello Bytes, I am having trouble copying a value from an open record in a recordset into a form. I have a form frmOutreachReferral that is filled in after a Site Visit has been performed and it...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.