Will it identify the record distinctly? Because I imagine you won't want to be updating other records with same field values.
Private Sub CheckError(ByVal lngRecordsAffected As Long, _
ByVal lngExpected As Long, ByVal strDescription As String)
If lngRecordsAffected <> lngExpected Then
Call RaiseError(strDescription)
End If
End Sub
Private Sub RaiseError(ByVal strDescription As String)
Call Err.Raise(vbObjectError + 1024, , strDescription)
End Sub
Private Sub ExecuteCommand(ByVal command As ADODB.command, _
ByVal strCommandText As String)
Dim lngRecordsAffected As Long
Dim strDescription As String
command.CommandText = strCommandText
On Error Resume Next
Call command.Execute(RecordsAffected:=lngRecordsAffected, _
Options:=CommandTypeEnum.adCmdText Or ExecuteOptionEnum.adExecuteNoRecords)
strDescription = IIf(Err.Number <> 0, Err.Description, vbNullString)
On Error GoTo 0
Call CheckError(lngRecordsAffected, 1, strDescription)
End Sub
Public Sub UpdateRecord(ByVal command As ADODB.command, ByVal strTableName As String, _
ByVal strField As String, ByVal strValue As String, ByVal lngKey As Long)
Dim strCommandText As String
strCommandText = "UPDATE " & strTableName & _
" SET " & strField & "=" & strValue & _
" WHERE [ID] =" & CStr(lngKey) & ";"
Call ExecuteCommand(command, strCommandText)
End Sub
I would put the key in the listbox but make that column zero width.
Public Sub Example()
Dim objCommand As ADODB.command
Dim strConnection As String
Dim strProvider As String
Dim strSQL As String
If Val(Application.Version) >= 12 Then
strProvider = "Microsoft.ACE.OLEDB.12.0"
Else
strProvider = "Microsoft.Jet.OLEDB.4.0"
End If
strConnection = "Provider=" & strProvider & ";" & _
"Data Source=" & DBpath & ";" & _
"Persist Security Info=False"
strSQL = "UPDATE blah blah"
With objCommand
.ActiveConnection = strConnection
.CommandText = strSQL
Call .Execute(RecordsAffected:=lngRecordsAffected, _
Options:=CommandTypeEnum.adCmdText Or ExecuteOptionEnum.adExecuteNoRecords)
End With
End Sub
'Neue Datenbank Verbindung aufbauen
Set jCN = New ADODB.Connection
jCN.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Path & ";"
TableName = "NOS_Material_Uebersicht"
'Neues Command
Set jCom = New ADODB.Command
jQuery = "UPDATE " & TableName & _
" SET [Brand]=" & Brand & "25, " & _
"[Produktname]=" & Produktname & "Works" & _
" WHERE [MaterialFarbe]=" & Material & Farbe & ";"
jCom.ActiveConnection = jCN
jCom.CommandText = jQuery
Call jCom.Execute(RecordsAffected:=lngRecordsAffected, _
Options:=CommandTypeEnum.adCmdText Or ExecuteOptionEnum.adExecuteNoRecords)
'Verbindungen schliessen
Set jCom = Nothing
jCN.Close
Set jCN = Nothing