SQL in vba. Find if record already exists...

paulpan

New Member
Joined
Sep 24, 2007
Messages
23
Hi

I want to find if a signoff already exists and if it does replace the record with new data. I am stuck on the first part of it. The procedure falls over on noDUP (I want it to return an integer, not a string). I was going to say that if noDUP =1 then refresh the recordset. If no then just addNew recordset set....

Please help...

Const adOpenStatic = 3
Const adLockOptimistic = 3

Dim irow As Integer
Dim sreqd As String
Dim objConnection As Object
Dim objRecordSet As Object

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")


objConnection.Open _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source = C:\Documents and Settings\Desktop\Sign Off Database.mdb"

objRecordSet.Open "SELECT * FROM tblSignOffs", _
objConnection, adOpenStatic, adLockOptimistic

'***********************************
Dim noDUP As Integer

noDUP = "SELECT * FROM tblSignOffs GROUP BY tblSignOffs.[3_NAVDate], tblSignOffs.[1_FundName] HAVING (((tblSignOffs.[3_NAVDate])=#'6/25/2008'#) AND ((tblSignOffs.[1_FundName])='PP01'))"
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I hope I'm not in over my head here, but you need to query the database and see if it returns a record. Here, you are simply trying to assign a string value to an integer variable which is doomed from the start.

Something like:
Code:
'-------------------
Dim strSQL as String
Dim blnRecordFound as Boolean
strSQL = "SELECT * FROM tblSignOffs" 'You would use the whole SQL text here
Call objRecordSet(strSQL, objConnection, adOpenStatic, adLockOptimistic)
If (objRecordSet.EOF and objRecordSet.BOF) Then
    blnRecordFound = False
Else
    blnRecordFound = True
End If

If objRecordSet.State And ObjectStateEnum.adStateOpen) Then objRecordset.Close 'Close recordset
Set objRecordSet = Nothing

If blnRecordFound Then
    '~~Do this
Else
    '~~Do this
End If
'-------------------

Hope this helps,
AB
 
Upvote 0

Forum statistics

Threads
1,215,544
Messages
6,125,438
Members
449,225
Latest member
mparcado

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top