Hi everyone, hope you’re having a great day.
The following code exports an Excel table with Job codes to a database table.
The code works and I’ve tested it. However, I still have a problem, and I’m not sure how to proceed from here. The problem is: I don’t want the program to write an entry twice. For example, if a “JobCode” already exists in the database, I want the old entry deleted in the database and new entry copied. Any suggestions?
(NOTE: SQL_INSERT_StringBuilder is a class somebody created for the purposes of constructing SQL "INSERT" statements)
Private Sub OKbutton_Click()
'On Error GoTo Errorhandler
Dim WorksheetNumber As Integer
Dim wb As Object 'Excel.Workbook
Dim datecomp As DataTypeEnum
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim xSQLbuilder As SQL_INSERT_StringBuilder
Dim TimeRange As Excel.Range
Dim JobcodeRange As Excel.Range
Dim i As Long
Dim sqlstr
Dim wbstring As String
Dim DBAddress As String
Dim LoginString As String
Dim workbookNeedsToBeClosed As Boolean
Dim wbtest As Excel.Workbook
DBAddress = "C:\Documents and Settings\Chris\My Documents \Timesheets.mdb"
wbstring = FileNameTextBox.Text
Set conn = New ADODB.Connection
For Each wbtest In Excel.Workbooks
If wbtest.FullName = wbstring Then
Set wb = wbtest
isopen = True
Exit For
End If
Next
If isopen = False Then
Set wb = ExcelProcess.Workbooks.Open(wbstring)
workbookNeedsToBeClosed = True
End If
wb.Activate
LoginString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " _
& DBAddress & ";Jet OLEDB:System database=" _
& ";User Id=" & "admin" _
& "; Password=" & ""
conn.ConnectionString = LoginString
conn.Open
Set JobcodeRange = wb.Sheets("Job codes").Range("JobCodes")
Set xSQLbuilder = New SQL_INSERT_StringBuilder
xSQLbuilder.TableName = "JobCodes"
With xSQLbuilder.ColumnNames
.Add "[Date]"
.Add "Client"
.Add "JobCode"
.Add "Type"
.Add "AorB"
.Add "JobDescription"
.Add "Country"
.Add "ClientContact"
.Add "ShortcutToProposal"
.Add "ProjectLeader"
.Add "JobStatus"
.Add "InvoiceSent"
.Add "PaymentReceived"
End With
For i = 1 To wb.Sheets("Job codes").Range("JobCodes").Rows.Count
With xSQLbuilder
Set .ColumnValues = New Collection
With .ColumnValues
.Add FormatDateForSQL(JobcodeRange.Cells(i, 1).Value, conn)
.Add "'" & CStr(JobcodeRange.Cells(i, 2).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 4).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 5).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 6).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 7).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 8).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 9).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 10).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 11).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 12).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 13).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 14).Value) & "'"
End With
End With
conn.Execute xSQLbuilder.Output
Next i
conn.Close
Set conn = Nothing
If Not xSQLbuilder Is Nothing Then
xSQLbuilder.Delete
Set xSQLbuilder = Nothing
End If
'Tidying up Excel connection
If workbookNeedsToBeClosed Then
wb.Close
End If
Set wb = Nothing
'fMainForm.CloseExcelProcess
'SetMicePointers vbDefault
'Me.Hide
Unload Me
Exit Sub
Errorhandler:
Unload Me
End Sub
The following code exports an Excel table with Job codes to a database table.
The code works and I’ve tested it. However, I still have a problem, and I’m not sure how to proceed from here. The problem is: I don’t want the program to write an entry twice. For example, if a “JobCode” already exists in the database, I want the old entry deleted in the database and new entry copied. Any suggestions?
(NOTE: SQL_INSERT_StringBuilder is a class somebody created for the purposes of constructing SQL "INSERT" statements)
Private Sub OKbutton_Click()
'On Error GoTo Errorhandler
Dim WorksheetNumber As Integer
Dim wb As Object 'Excel.Workbook
Dim datecomp As DataTypeEnum
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim xSQLbuilder As SQL_INSERT_StringBuilder
Dim TimeRange As Excel.Range
Dim JobcodeRange As Excel.Range
Dim i As Long
Dim sqlstr
Dim wbstring As String
Dim DBAddress As String
Dim LoginString As String
Dim workbookNeedsToBeClosed As Boolean
Dim wbtest As Excel.Workbook
DBAddress = "C:\Documents and Settings\Chris\My Documents \Timesheets.mdb"
wbstring = FileNameTextBox.Text
Set conn = New ADODB.Connection
For Each wbtest In Excel.Workbooks
If wbtest.FullName = wbstring Then
Set wb = wbtest
isopen = True
Exit For
End If
Next
If isopen = False Then
Set wb = ExcelProcess.Workbooks.Open(wbstring)
workbookNeedsToBeClosed = True
End If
wb.Activate
LoginString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " _
& DBAddress & ";Jet OLEDB:System database=" _
& ";User Id=" & "admin" _
& "; Password=" & ""
conn.ConnectionString = LoginString
conn.Open
Set JobcodeRange = wb.Sheets("Job codes").Range("JobCodes")
Set xSQLbuilder = New SQL_INSERT_StringBuilder
xSQLbuilder.TableName = "JobCodes"
With xSQLbuilder.ColumnNames
.Add "[Date]"
.Add "Client"
.Add "JobCode"
.Add "Type"
.Add "AorB"
.Add "JobDescription"
.Add "Country"
.Add "ClientContact"
.Add "ShortcutToProposal"
.Add "ProjectLeader"
.Add "JobStatus"
.Add "InvoiceSent"
.Add "PaymentReceived"
End With
For i = 1 To wb.Sheets("Job codes").Range("JobCodes").Rows.Count
With xSQLbuilder
Set .ColumnValues = New Collection
With .ColumnValues
.Add FormatDateForSQL(JobcodeRange.Cells(i, 1).Value, conn)
.Add "'" & CStr(JobcodeRange.Cells(i, 2).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 4).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 5).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 6).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 7).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 8).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 9).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 10).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 11).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 12).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 13).Value) & "'"
.Add "'" & CStr(JobcodeRange.Cells(i, 14).Value) & "'"
End With
End With
conn.Execute xSQLbuilder.Output
Next i
conn.Close
Set conn = Nothing
If Not xSQLbuilder Is Nothing Then
xSQLbuilder.Delete
Set xSQLbuilder = Nothing
End If
'Tidying up Excel connection
If workbookNeedsToBeClosed Then
wb.Close
End If
Set wb = Nothing
'fMainForm.CloseExcelProcess
'SetMicePointers vbDefault
'Me.Hide
Unload Me
Exit Sub
Errorhandler:
Unload Me
End Sub