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

VB Take a value from Excel to delete row from Access table

Hello all

I am currently trying to develop a script that will take a value from an Excel spreadsheet cell and use that to run a query against an Access table to delete all rows that match the cell.

I have tried to do this by first using a recordset and then secondly using a db.Execute (sqlstring).

The idea of using the recordset was that I thought it would return all of the records matched, however it just deletes the 1 row not all of the entries that match.

Is this just the way I have opened the recordset?

For the db.Execute I'm getting a 3061 Error - Too few parameters!

Any help truly appreciated.

Expand|Select|Wrap|Line Numbers
  1. ' exports data from the active worksheet to a table in an Access database
  2. ' exports data from the active worksheet to a table in an Access database
  3.  
  4. Dim db As DAO.Database      ' PAR History Database location/file
  5. Dim rs As DAO.Recordset     ' Table name
  6. 'Dim rs As Recordset
  7. Dim cl As Long              ' Current Line counter
  8. Dim vcurpar As String       ' Current PAR number (N2)
  9.  
  10.  
  11. vcurpar = "Notifications!N2"
  12.  
  13.  
  14.     Application.StatusBar = "Open PAR History Database"
  15.  
  16.  
  17.     ' open the database
  18.     Set db = OpenDatabase("C:\My Stuff\Projects\GCUK\PAR Change Control\PARHistory.mdb")
  19.  
  20.     ' Delete any previously PAR with this number from history.
  21.     Application.StatusBar = "Checking and removing if PAR has been archieved previously"
  22.     vtSql = "DELETE * FROM CircuitHistory WHERE parnumber = " & vcurpar & ";"
  23.  
  24.     'db.Execute (vtSql)
  25.  
  26.  
  27.  
  28.     Set rs = db.OpenRecordset("SELECT * FROM CircuitHistory WHERE CircuitHistory.parnumber = 'PAR123456'")
  29.     If Not rs.EOF Then
  30.         MsgBox (rs.RecordCount)
  31.  
  32.         rs.Delete
  33.     End If
  34.  
  35.     rs.Close
  36.  
  37.  
  38.  
  39.  
Feb 13 '07 #1
1 2304
willakawill
1,646 1GB
Hi. A couple of things. When you are not assigning a return value, don't enclose your parameters in brackets as in MsgBox (rs.RecordCount) or db.Execute (vtSql)
Secondly, in the two sql statements you have alternately referenced parnumber as numeric and textual. As the textual version seems to work in the second case here is a change to the code that will most likely work in the first case:
Expand|Select|Wrap|Line Numbers
  1. vtSql = "DELETE * FROM CircuitHistory WHERE parnumber = '" & vcurpar & "';"
Good luck
Feb 15 '07 #2

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

Similar topics

8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
3
by: Mads Petersen | last post by:
I'm stuck in this code. Hope you can and will help me. I launch it from excel. I have made the following code work, but not as i whant. I need the ranges to be working with something like xlDown....
7
by: Smitty | last post by:
I have a function that imports an Excel file into an Access table using SQL. I then close the OleDataReader and the OleDbConnection, then dispose the OleDbCommand, then OleDbConnection. The calling...
21
by: bobh | last post by:
Hi All, In Access97 I have a table that's greater than 65k records and I'm looking for a VBA way to export the records to Excel. Anyone have vba code to export from access to excel and have the...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
12
by: slinky | last post by:
Can an Excel spreadsheet or a section of one be embedded into an Access form and serve as a subform from which other parts of the Access form can get data? Thanks!
2
by: John Bartley K7AAY | last post by:
When I output a table's values to XLS, one value in a very small table, and only one value, is changed. Here are the values in the table, tblLevel. LEVEL H-14 0 1 1.1
0
by: Shootah | last post by:
Hi, I have succeeded in adding automated relationships with refference tables after importing an excel file created from a query to an Access database. However I have the following problem: ...
0
by: JFKJr | last post by:
I have an excel file, which has columns C and D grouped together, I am trying to delete blank columns and rows from the excel file, ungroup the columns and import the file to MS Access using Access...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.