473,655 Members | 3,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error opening a recordset in MS Access 2002, vba script

4 New Member
Good morning, all.

I am creating an Access 2002 application to run on XP clients and I'm struggling with the task of testing whether a single-field record exists before allowing the user to add it to a look up table. I considered creating a unique index on the field in the table, but the users here would not know how to handle the error message. From there, I moved to creating a recordset that tests for the presence of a matching record before performing the insert, but I have not been able to open the recordset.

I received the error message "Run-time error 3061. Too few parameters. Expected 1." on the line of code, below, where I try to open the recordset.

Can anyone offer advice on how to open the recordset, or another way to test for duplication of data before allowing the insert of the new record?

Thanks in advance, Mark


The code for the recordset follows...

Expand|Select|Wrap|Line Numbers
  1. Dim newCat, status As String
  2. newCat = Text_newCat 'set variable equal to text on screen
  3. status = "Checking if Category already exists..."  ' tell the user what's up
  4. Text_status = status  ' update text box with the message
  5.  
  6. Dim db As database
  7. Dim rs As Recordset
  8. Dim Occurances As Integer
  9. Dim countsql As String
  10.  
  11. 'open record set of household categories matching newCat
  12. countsql = "SELECT [Household Category].HHC_Desc, Count([Household Category].HHC_Key) AS CountOfHHC_Key " & _
  13.            "FROM [Household Category] " & _
  14.            "GROUP BY [Household Category].HHC_Desc " & _
  15.            "HAVING ((([Household Category].HHC_Desc)=[Forms]![Add Household Category]![Text_newCat]));"
  16.  
  17. Set db = CurrentDb
  18. Set rs = db.openrecordset(countsql)
  19. rs.MoveLast
  20. Occurances = rs.RecordCount
  21.  
  22. If Occurances = 0 Then
  23. Text_status = "Adding New Household Category..."
  24. Dim sql1, sql2 As String
  25. sql2 = "INSERT INTO [Household Category] ( HHC_Desc ) select '" & newCat & "';"
  26. DoCmd.RunSQL sql2
  27. Text_status = "New Household Category added..."
  28. Else
  29.     Text_status = "This Household Category already exists."
  30. End If
Jul 6 '07 #1
4 2046
MMcCarthy
14,534 Recognized Expert Moderator MVP
Try changing the first query as follows:

Expand|Select|Wrap|Line Numbers
  1. countsql = "SELECT [Household Category].HHC_Desc, Count([Household Category].HHC_Key) AS CountOfHHC_Key " & _
  2. "FROM [Household Category] " & _
  3. "WHERE [Household Category].HHC_Desc=[Forms]![Add Household Category]![Text_newCat] " & _
  4. "GROUP BY [Household Category].HHC_Desc;"
As for the syntax of the INSERT Query ...

Expand|Select|Wrap|Line Numbers
  1. sql2 = "INSERT INTO [Household Category] ( HHC_Desc ) select '" & newCat & "';"
What is a sample of the text value normally behind newCat?
Jul 7 '07 #2
breadhead
4 New Member
The newCat text would be something like...

Christmas card recipient
Small Business Loan luncheon
member of the Country Club

These values will be stored in a two field table, with the first column an autonumbered key and the second a 150 character text field.

I'll try your changes this morning and let you know how it goes. Thanks for your advice.

Mark


Try changing the first query as follows:

Expand|Select|Wrap|Line Numbers
  1. countsql = "SELECT [Household Category].HHC_Desc, Count([Household Category].HHC_Key) AS CountOfHHC_Key " & _
  2. "FROM [Household Category] " & _
  3. "WHERE [Household Category].HHC_Desc=[Forms]![Add Household Category]![Text_newCat] " & _
  4. "GROUP BY [Household Category].HHC_Desc;"
As for the syntax of the INSERT Query ...

Expand|Select|Wrap|Line Numbers
  1. sql2 = "INSERT INTO [Household Category] ( HHC_Desc ) select '" & newCat & "';"
What is a sample of the text value normally behind newCat?
Jul 9 '07 #3
breadhead
4 New Member
Try changing the first query as follows:

Expand|Select|Wrap|Line Numbers
  1. countsql = "SELECT [Household Category].HHC_Desc, Count([Household Category].HHC_Key) AS CountOfHHC_Key " & _
  2. "FROM [Household Category] " & _
  3. "WHERE [Household Category].HHC_Desc=[Forms]![Add Household Category]![Text_newCat] " & _
  4. "GROUP BY [Household Category].HHC_Desc;"
I made this change, but I am still receiving the same error message as previously stated. It seems to me I'm doing something wrong in the initiation of the recordset. This is the first instance of this I've done, and all the help files I have seen include connection objects pointing to external data sources, not within the same Access mdb file as I am trying to do.

Still seeking guidance,
Mark
Jul 9 '07 #4
MMcCarthy
14,534 Recognized Expert Moderator MVP
I made this change, but I am still receiving the same error message as previously stated. It seems to me I'm doing something wrong in the initiation of the recordset. This is the first instance of this I've done, and all the help files I have seen include connection objects pointing to external data sources, not within the same Access mdb file as I am trying to do.

Still seeking guidance,
Mark
Change your second query to ...

sql2 = "INSERT INTO [Household Category] ( HHC_Desc ) VALUES ('" & newCat & "');"
Jul 9 '07 #5

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

Similar topics

5
2370
by: bhieb | last post by:
I have a data control that I write a sql statement on the fly and populate the recordset like so. Set Rst = Db.OpenRecordset("SELECT DISTINCT * FROM Source_UnitMaster .... join and where clause") set dat_Equip.Recordset = Rst (this is a DAO recordset) This gives me a "operation is not supported by this object" at the second line.
3
10168
by: KemperR | last post by:
Hello Experts outhere, may be someone can tell me whats going wrong with my ADOX trial. I have an Access 2002 database with some tables and queries (views) The code listed below works well up to the point where I want to add the new view to the views collection. I get Runtime error 3001 which is telling me "Arguments are of wrong type,are out of acceptable range or conflict with one another"
4
5528
by: MNC | last post by:
I'm using Access2002, and can't seem to get an updateable recordset going :-( What am I doing wrong, here's the code. The form's controls are not locked, the recordset type is Dynaset (changing to Dynaset inconsistent updates does not work), I'm allowing edits, and I'm at a loss ... Please help! Option Compare Database
6
4739
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much appreciated. Thanks in advance
5
7849
by: j.mandala | last post by:
Someone is trying to run my Access 2002 database under Access 2003. He has had a number of problems: 1) i used the MSComCt2.ocx for it's Date and Time picker. I can't find it under 2003. Do I need to send it to them? 2) I have a function to fill a table with values, that store the page and column numbers of a display of staff members. I had him check the link to Microsoft DAO 3.6 Object Library under references, but it still won't...
1
5104
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 wrong. I had posted this earlier along with another problem. I got answeres for that, but not this one; so I am reposting, hoping someone would explain When I try the following code it gives the error message "compile error- type mismatch" on...
4
3328
by: dhcomcast | last post by:
We're starting to use Oracle for the back-end instead of a separate Access .mdb file for the data and everything as gone surprisingly well so far. We are learning Oracle as we go; Yikes! But we have very patient DBA's at the main office to answer newbie questions. Using Access 2002 with linked ODBC tables to Oracle 9 (9i? not sure), ODBC driver is SQORA32.DLL, verson 9.02.00.00. My "Error 3167: Record is deleted" comes when I use...
0
7708
by: gm | last post by:
Immediately after generating the Access application from the Source Safe project I get: "-2147467259 Could not use ''; file already in use." If Access database closed and then reopened I get: "-2147467259 The database has been place in a state by user 'Admin' on machine ..... that prevents it from being opened or locked."
1
7913
by: amindi | last post by:
Hi, I wrote a VB6 program to read some data records from a Ms Access database and to write them into a SQL server database.(I use Ms Access 2000 and SQL server 2000).After reading each record in Access database, I update a text type datafield of that record in Access database to 'Y' (to mark the perticular record as a copied record)(The program does the updation). I tested the program several times by using a seperate copy of the same database...
0
8380
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.