473,386 Members | 1,803 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,386 software developers and data experts.

Problems using OpenRecordSet with a prebuilt query

I have a form that i want to be able to reassign a specific piece of software to a different machine.

Form specs:
Software Title: cbotitle
CDKEY: cbocdkey
Old Assignment: cbooldassignment
New Assignment: cbonewassignment


I've built a query that pulls a list of all the computers the software is currently assigned to dynamically by gettings its criteria from the current form.

If i open the query manually after completing the form everythign works fine.

SQL for qryASSIGNCHANGE:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblSOFTWARE.COMPUTER FROM tblSOFTWARE WHERE (((tblSOFTWARE.KEY)=[forms]![frmASSIGNSOFTWARE].[cbocdkey].[value]));
Then on my submit button I want to find the first value in the query that matches the cbooldassignment and change it to the new. I used findfirst because there could be more than one UNASSIGNED for example and i dont want it to change all of them to the new value.

Code for Submit Button:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command8_Click()
  2.  
  3. Dim db As DAO.Database
  4. Dim rcd As DAO.Recordset
  5. Dim newassignment As String
  6. Dim oldassignment As String
  7.  
  8. newassignment = Me.cbonewassignment.Value
  9. oldassignment = Me.cbooldassignment.Value
  10.  
  11. Set db = CurrentDb
  12. Set rcd = db.OpenRecordset("qryASSIGNCHANGE")
  13.  
  14. rcd.FindFirst ([COMPUTER] = oldassignment)
  15. rcd.Edit
  16. rcd![COMPUTER] = newassignment
  17. rcd.Update
  18. rcd.Close
  19.  
  20. cbotitle.Value = Null
  21. cbocdkey.Value = Null
  22. cbooldassignment.Value = Null
  23. cbonewassignment.Value = Null
  24.  
  25. End Sub
I Keep getting Error 3061: Too few parameters. Expected 1

I have tried all kinds of things even specifying the parameter with querydefs but couldnt get that to work either. Does anyone know what my problem could be and what I might need to do differently?
Nov 9 '07 #1
12 5492
JKing
1,206 Expert 1GB
I have a form that i want to be able to reassign a specific piece of software to a different machine.

Form specs:
Software Title: cbotitle
CDKEY: cbocdkey
Old Assignment: cbooldassignment
New Assignment: cbonewassignment


I've built a query that pulls a list of all the computers the software is currently assigned to dynamically by gettings its criteria from the current form.

If i open the query manually after completing the form everythign works fine.

SQL for qryASSIGNCHANGE:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblSOFTWARE.COMPUTER FROM tblSOFTWARE WHERE (((tblSOFTWARE.KEY)=[forms]![frmASSIGNSOFTWARE].[cbocdkey].[value]));
Then on my submit button I want to find the first value in the query that matches the cbooldassignment and change it to the new. I used findfirst because there could be more than one UNASSIGNED for example and i dont want it to change all of them to the new value.

Code for Submit Button:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command8_Click()
  2.  
  3. Dim db As DAO.Database
  4. Dim rcd As DAO.Recordset
  5. Dim newassignment As String
  6. Dim oldassignment As String
  7.  
  8. newassignment = Me.cbonewassignment.Value
  9. oldassignment = Me.cbooldassignment.Value
  10.  
  11. Set db = CurrentDb
  12. Set rcd = db.OpenRecordset("qryASSIGNCHANGE")
  13.  
  14. rcd.FindFirst ([COMPUTER] = oldassignment)
  15. rcd.Edit
  16. rcd![COMPUTER] = newassignment
  17. rcd.Update
  18. rcd.Close
  19.  
  20. cbotitle.Value = Null
  21. cbocdkey.Value = Null
  22. cbooldassignment.Value = Null
  23. cbonewassignment.Value = Null
  24.  
  25. End Sub
I Keep getting Error 3061: Too few parameters. Expected 1

I have tried all kinds of things even specifying the parameter with querydefs but couldnt get that to work either. Does anyone know what my problem could be and what I might need to do differently?
Try adding quotes in the findfirst line.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command8_Click()
  2.  
  3. Dim db As DAO.Database
  4. Dim rcd As DAO.Recordset
  5. Dim newassignment As String
  6. Dim oldassignment As String
  7.  
  8. newassignment = Me.cbonewassignment.Value
  9. oldassignment = Me.cbooldassignment.Value
  10.  
  11. Set db = CurrentDb
  12. Set rcd = db.OpenRecordset("qryASSIGNCHANGE")
  13.  
  14. rcd.FindFirst ("[COMPUTER] = '" & oldassignment & "'")
  15. rcd.Edit
  16. rcd![COMPUTER] = newassignment
  17. rcd.Update
  18. rcd.Close
  19.  
  20. cbotitle.Value = Null
  21. cbocdkey.Value = Null
  22. cbooldassignment.Value = Null
  23. cbonewassignment.Value = Null
  24.  
  25. End Sub
Hope this helps.
If you're still receiving the error let me know which line is the offending line.

Jared
Nov 10 '07 #2
Try adding quotes in the findfirst line.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command8_Click()
  2.  
  3. Dim db As DAO.Database
  4. Dim rcd As DAO.Recordset
  5. Dim newassignment As String
  6. Dim oldassignment As String
  7.  
  8. newassignment = Me.cbonewassignment.Value
  9. oldassignment = Me.cbooldassignment.Value
  10.  
  11. Set db = CurrentDb
  12. Set rcd = db.OpenRecordset("qryASSIGNCHANGE")
  13.  
  14. rcd.FindFirst ("[COMPUTER] = '" & oldassignment & "'")
  15. rcd.Edit
  16. rcd![COMPUTER] = newassignment
  17. rcd.Update
  18. rcd.Close
  19.  
  20. cbotitle.Value = Null
  21. cbocdkey.Value = Null
  22. cbooldassignment.Value = Null
  23. cbonewassignment.Value = Null
  24.  
  25. End Sub
Hope this helps.
If you're still receiving the error let me know which line is the offending line.

Jared

Still receive the same error. Its the OpenRecordset line that gives me the error.
For some reason it's not getting the parameter from the form. If the form is open and filled out and i manually open the query it shows the results properly, if the form is not open the query will pop up and ask me for the value of the cdkey from the form. I just cannot figure out why it's not able to see the value from the form when i run this with a button onclick.

Is there any way to set the value from the form to a public variable i guess. So that the query is not actually looking for the form to get its parameter value but instead the variable and I could set the combo box on the form to update the variable onchange. ??
Nov 12 '07 #3
JKing
1,206 Expert 1GB
Try putting your query into a string and then passing the string into the openrecordset command.

If you are unsure how to do this post the sql for your query and I will help you with building the string.
Nov 12 '07 #4
Try putting your query into a string and then passing the string into the openrecordset command.

If you are unsure how to do this post the sql for your query and I will help you with building the string.
Ok, I now have this. Still getting "Too Few Parameters: Expected 1"

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim qdf As DAO.QueryDef
  3. Dim rst As DAO.Recordset
  4. Dim newassignment As String
  5. Dim oldassignment As String
  6. Dim cdkey As String
  7. Dim strTestQuery As String
  8. Dim strSQL As String
  9.  
  10. strTestQuery = "qryTEST"
  11. newassignment = Me.cbonewassignment.Value
  12. oldassignment = Me.cbooldassignment.Value
  13. cdkey = Me.cbocdkey.Value
  14.  
  15. Set db = CurrentDb
  16.  
  17. strSQL = ("SELECT [COMPUTER] FROM tblSOFTWARE WHERE [KEY] = [cdkey]")
  18. Set qdf = db.CreateQueryDef(strTestQuery, strSQL)
  19.  
  20.  
  21.  
  22. Set rst = qdf.OpenRecordset
  23. rst.FindFirst ("[COMPUTER] = '" & oldassignment & "'")
  24. rst.Edit
  25. rst![COMPUTER] = newassignment
  26. rst.Update
  27. rst.Close
Nov 12 '07 #5
jrayjr
15
Ok, I now have this. Still getting "Too Few Parameters: Expected 1"

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim qdf As DAO.QueryDef
  3. Dim rst As DAO.Recordset
  4. Dim newassignment As String
  5. Dim oldassignment As String
  6. Dim cdkey As String
  7. Dim strTestQuery As String
  8. Dim strSQL As String
  9.  
  10. strTestQuery = "qryTEST"
  11. newassignment = Me.cbonewassignment.Value
  12. oldassignment = Me.cbooldassignment.Value
  13. cdkey = Me.cbocdkey.Value
  14.  
  15. Set db = CurrentDb
  16.  
  17. strSQL = ("SELECT [COMPUTER] FROM tblSOFTWARE WHERE ([KEY]) = [cdkey]")
  18. Set qdf = db.CreateQueryDef(strTestQuery, strSQL)
  19.  
  20.  
  21.  
  22. Set rst = qdf.OpenRecordset
  23. rst.FindFirst ("[COMPUTER] = '" & oldassignment & "'")
  24. rst.Edit
  25. rst![COMPUTER] = newassignment
  26. rst.Update
  27. rst.Close

I think the problem is you need to break out of the SQL statement to pull in your cdkey array because its trying to get it from your table and its not there.

Try your SQL statment like this.
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT [COMPUTER] FROM tblSOFTWARE WHERE ([KEY]) = " & cdkey & ""
  2.  
Or

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT tblSOFTWARE.COMPUTER FROM tblSOFTWARE WHERE (tblSOFTWARE.KEY) = " & cdkey & ""
  2.  
Or

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT tblSOFTWARE.COMPUTER FROM tblSOFTWARE WHERE (tblSOFTWARE.KEY) = " & Me.cbocdkey & ""
  2.  
Gave 3 examples hope they work for you.
Nov 12 '07 #6
Yes i was just about to post up here that i figured it out.. This is what i used.

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT [COMPUTER] FROM tblSOFTWARE WHERE [KEY] = '" & cdkey & "'"

Thank you so much for the help.
Nov 12 '07 #7
jrayjr
15
Yes i was just about to post up here that i figured it out.. This is what i used.

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT [COMPUTER] FROM tblSOFTWARE WHERE [KEY] = '" & cdkey & "'"

Thank you so much for the help.

No problem, Glad its working for you.
Nov 12 '07 #8
Hi,

I've got a similar problem to the one in this post which was solved.

My error returned is "Invalid Argument" and it points to the line where OpenRecordset is used.

My code is: -

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command26_Click()
  2.  
  3. Dim test As Database
  4. Dim Rs As DAO.Recordset
  5. Dim Msg As String
  6. Dim bFlag As Boolean
  7.  
  8. Set Rs = CurrentDb.OpenRecordset("SELECT Time_Slot, Count([Bookings])+1 AS Total FROM qryBookedHolidaySlots WHERE (((Time_Slot) Between Forms!tblBooking!Booking_StartDtm And Forms!tblBooking!Booking_EndDtm)) GROUP BY Time_Slot;", dbOpenStatic, dbReadOnly)
  9.  
  10. If Not Rs.EOF And Not Rs.EOF Then
  11.    Do Until Rs.EOF
  12.        If Booking_Type = "Hol" And Rs("Total") >= 4 Then
  13.           Msg = Msg & "Time Slot :" & Rs("Time_Slot") & " Full" & vbCrLf
  14.           bFlag = True
  15.        End If
  16.        Rs.MoveNext
  17.    Loop
  18.    Rs.Close
  19. End If
  20. Set Rs = Nothing
  21.  
  22. If bFlag = True Then
  23.    MsgBox "The following time slots are fully booked:" & vbCrLf & vbCrLf & Msg, vbExclamation + vbOKOnly, "Bookings Conflict"
  24.         DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  25. End If
  26.  
  27. End Sub
Any help would be very much appreciated as I've been stuck for 3 days trying to solve it and the help on the Microsoft website regarding OpenRecordset isn't helpful at all.

Thanks in advance
Jun 19 '08 #9
Stewart Ross
2,545 Expert Mod 2GB
Hi Wildster. Problem is the references to form fields within your SQL string, which will not be recognised as valid when the recordset open takes place. To resolve this you need to include the value of the form fields, not their names.

A small complication here is that I am not sure what the type of the underlying data in the controls involved is. If it is times (as these are time slots) you may have to use explicit date/time delimiters - '#' before and after each reference to the value to make it work. If the control values are strings, you would need single quotes. If they are numbers, no delimiters will be required. The three alternatives are listed below, numeric then string then date/time:

Expand|Select|Wrap|Line Numbers
  1. NUMERIC VERSION
  2. SELECT Time_Slot, Count([Bookings])+1 AS Total FROM qryBookedHolidaySlots WHERE (((Time_Slot) Between " & Forms!tblBooking!Booking_StartDtm & " And " & Forms!tblBooking!Booking_EndDtm & ")) GROUP BY Time_Slot;"
  3.  
  4. STRING VERSION
  5. SELECT Time_Slot, Count([Bookings])+1 AS Total FROM qryBookedHolidaySlots WHERE (((Time_Slot) Between '" & Forms!tblBooking!Booking_StartDtm & "' And '" & Forms!tblBooking!Booking_EndDtm & "')) GROUP BY Time_Slot;"
  6.  
  7. TIME VERSION
  8. SELECT Time_Slot, Count([Bookings])+1 AS Total FROM qryBookedHolidaySlots WHERE (((Time_Slot) Between #" & Forms!tblBooking!Booking_StartDtm & "# And #" & Forms!tblBooking!Booking_EndDtm & "#)) GROUP BY Time_Slot;"
-Stewart
Jun 19 '08 #10
Thanks for the post, it makes sense what you're saying. Although I've completely stripped the query back to something very basic and it still doesn't like OpenRecordset. There is something else more underlying which is causing the error (the solution you gave probably solved what would have been a preceeding error once this one was corrected).

I've stripped the code back to: -

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim Rs As DAO.Recordset
  3.  
  4. Set db = CurrentDb
  5.  
  6. Set Rs = db.OpenRecordset("SELECT * FROM tblBooking", dbOpenDynaset)
I'm getting runtime error 91: Object variable or With block variable not set.

The only two variables I'm using are db and Rs which I both have declared and set, is there something that I'm doing wrong? i.e. need to open the db first, declare something else etc

I've been stuck on this problem all week now and no matter how many help files, Microsoft support pages etc. I look at I can't seem to find a solution. If anyone can offer help then it would be very much appreciated. I've even simplified the sql query to something really simple to get OpenRecordset to work but no luck.

Thanks
Jun 20 '08 #11
Stewart Ross
2,545 Expert Mod 2GB
OK. There's nothing wrong with your stripped down code - and just to be absolutely certain belt and brace wise I tried it myself in an Access 2003 test DB on a table renamed tblBooking for the purpose.

I wonder if there is a conflicting project reference somewhere which is interfering with the correct interpretation of your DAO recordset?

Could you check your project references (from the VB editor select Tools, References) and make sure that there is only one reference to the Microsoft DAO Object Library ticked, and that nothing else looks suspicious? I will attach a screenshot of my own project references so you know what to look for if it is not obvious.

-Stewart
Attached Images
File Type: jpg ScreenHunter_51.jpg (11.9 KB, 244 views)
Jun 20 '08 #12
The references were the problem, thanks very much for your help
Jun 24 '08 #13

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

Similar topics

3
by: Reginald | last post by:
I am trying to create a query based on 1 of 3 date fields from a table called "T40". the first date is T40RbtDte, 2nd is T40RbtRecDte, 3rd is T40RbtDueDte. I have created a form to alow the user...
5
by: Scott | last post by:
I have a customer that had developed an Access97 application to track their business information. The application grew significantly and they used the Upsizing Wizard to move the tables to SQL...
5
by: Sunnyrain | last post by:
I am developing a program in Access 2000. I couldn't make OpenRecordset method work right. It's working when I opened a simple SQL query below in OpenRecordset. ..... Dim dbs As Database, rst...
0
by: Peter S | last post by:
Hi: I am trying to read a SQL Server text field (Access memo field) using connection.OpenRecordSet("qry with text field") rather than database.OpenRecordSet("qry with text field")
1
by: owengoodhew | last post by:
Guys I need your help/Advice... In my Access Database I have a query (lets say qry1) and in this query i have 2 fields for start and end date, which is provided by 2 Get functions. also i...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
1
by: EwanD | last post by:
I am trying to read through and process an Access Query using VBA. I have used the OpenRecordset method with parameters as below OpenRecordset(sSourceRecordset, dbOpenDynaset) Where...
4
by: Rick | last post by:
Access2003 in XP I'm using the code below to append any new records from (tbl_From_Mainframe) into (tbl_Appended_Data). It takes more than a minute to search 7000 records for a dozen new records....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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,...

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.