Getting error 1004 when calling refresh from within VBA

msaxton

New Member
Joined
Oct 7, 2005
Messages
3
I am getting an error 1004 when calling refresh of a query within VBA. If I refresh the query manually, it works. The VBA code reads a table and determines the highest notes sequence currently used. It then reads a list of notes to add to the table and one by one, updates the insert query and calls the refresh.

The strange things is that even when I get the error, the database shows the first notes records being added. Why would to work but then raise an error during the refresh?

What's even more confusing is I added On Error Resume Next right before the offending refresh and the code works just fine.

Anyone have any suggestions why this would be happening?


Below is the full code of the sub:

Sub UpdateSyteline()

Dim wbSourceWB As Workbook
Dim shDataSheet As Worksheet
Dim shCurNotesSheet As Worksheet


Dim lCurKey As Long
Dim lCurSeq As Long
Dim sSerialNumber As String


Dim lStartRow As Long
Dim lEndRow As Long
Dim lNumSerials As Long
Dim lFromRow As Long

Dim lFromCol As Long



Set wbSourceWB = ActiveWorkbook

Set shDataSheet = Worksheets("Data")
Set shCurNotesSheet = Worksheets("Current Notes")

' Refresh query to see current notes
wbSourceWB.Connections("CurrentJobNotes").Refresh

' Get Key and highest sequence for current notes
lCurKey = shCurNotesSheet.Range("C2").Value
lCurSeq = shCurNotesSheet.Range("E2").Value

' figure out how many Serial lines we have
shDataSheet.Range("C8").Select
lStartRow = ActiveCell.Row
If shDataSheet.Range("C9").Value = "" Then
lEndRow = lStartRow
Else
Selection.End(xlDown).Select
lEndRow = ActiveCell.Row
End If
lNumSerials = lEndRow - lStartRow + 1

' For each serial in master sheet
For lFromRow = lStartRow To lEndRow

' Get serial Number
sSerialNumber = shDataSheet.Cells(lFromRow, 2)
If sSerialNumber = "" Then sSerialNumber = SerialToJulian(Date)

' Add 1 to current sequence
lCurSeq = lCurSeq + 1

' Update insert SQL command
wbSourceWB.Connections("InsertJobNotes").ODBCConnection.CommandText = "Insert into vmain.notes (key, seq, new-paragraph, txt) " _
& " values (" & lCurKey & ", " & lCurSeq & ", True, '" & sSerialNumber & "')"
On Error Resume Next
'Refresh insert query to write to database
wbSourceWB.Connections("InsertJobNotes").Refresh
On Error GoTo 0
Next lFromRow

End Sub 'UpdateSyteline
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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