This is not currently working and if i add the line 'Msgbox rs.recordcount' then this comes back with the message '-1'.
However, when i add the line 'ActiveSheet.Range("A5").CopyFromRecordset (rs)' then all records are pasted in to my excel spreadsheet. can someone see what i am doing wrong?
Expand|Select|Wrap|Line Numbers
- Sub SubmitInterchange()
- Dim oConn As Object
- Dim sSQL As String
- Set rs = New ADODB.Recordset
- Dim strSql As String
- Set oConn = CreateObject("ADODB.Connection")
- oConn.Open = "Provider=sqloledb;" & _
- "Data Source=############;" & _
- "Initial Catalog=SysproCompany2;" & _
- "User Id=##;" & _
- "Password=#######"
- strSql = "SELECT StockCode FROM InvMaster"
- Set rs.ActiveConnection = oConn
- rs.Open strSql
- MsgBox rs.RecordCount
- 'ActiveSheet.Range("A5").CopyFromRecordset (rs)
- RowCount = rs.RecordCount
- Dim alldata() As Variant
- alldata = rs.GetRows(RowCount)
- Dim switch As Boolean
- Dim StringVal As String
- switch = False
- For n = 10 To 109
- StockCode = Worksheets("Interchange_Insert").Cells(n, 4).Value
- Barcode = Worksheets("Interchange_Insert").Cells(n, 5).Value
- If StockCode > 1 Then
- For i = 0 To RowCount - 1
- StringVal = CStr(alldata(0, i))
- StringVal = Trim(StringVal)
- If StringVal = StockCode Then
- switch = True
- End If
- Next i
- If switch = False Then
- MsgBox StockCode & " is an invalid stock code"
- Exit For
- End If
- switch = False
- MsgBox "StockCode:" & StockCode & " Barcode:" & Barcode & ""
- sSQL = "INSERT INTO InvMaster(StockCode, DrawOfficeNum) VALUES ('" & StockCode & "', '" & Barcode & "')"
- oConn.Execute sSQL
- End If
- Next n
- oConn.Close
- Set oConn = Nothing
- End Sub