Before hitting the sack, I'll give you the basic code I created. I pray that you can show me what I'm doing wrong.
God Bless you buddy!
Public Function VBAUpdateMethodBU(MODE As String, Tablename As String, Where1 As String, Key1 As Variant, ParamArray Arguments() As Variant)
If Trim(MODE) = "" Then
MsgBox "The MODE field must be filled in!"
Exit Function
ElseIf Trim(Tablename) = "" Then
MsgBox "The TableName field must be filled in!"
Exit Function
Else
MODE = Trim(UCase(MODE))
End If
Dim mArgs As Integer, QueryStr As String, mLen As Integer
' We need a way to create only the memvars that are required based on the user's input
' For now, until I can get some help, I am just setting up enough for 5 field/DataReplacements
' I need to be able to handle up to approximate 40-50 sets!
Dim F1 As String, F2 As String, F3 As String, F4 As String, F5 As String
Dim v1 As Variant, V2 As Variant, V3 As Variant, V4 As Variant, V5 As Variant
' Use UBound function to determine upper limit of ParamArray()
' Note that entering one field/data yields a UBound(Arguments())+1 value of 2
' entering two field/data yields a UBound(Arguments())+1 value of 4
' entering three field/data yields a UBound(Arguments())+1 value of 6, etc.
For mArgs = 0 To UBound(Arguments())
' The following allows up to 26 field udpates. Add more when confirmed that this works.
' Hopefully Danny and I will figure out a way create only the what we need on the fly based
' on what was entered into the ParamArray() above using the value of UBound(Arguments()) as a basis.
Select Case mArgs + 1
Case 1
F1 = Arguments(mArgs) ' First field value of ParamArray
Case 2
v1 = Arguments(mArgs) ' First data update value of ParamArray
Case 3
F2 = Arguments(mArgs) ' Second field value of ParamArray
Case 4
V2 = Arguments(mArgs) ' Second data update value of ParamArray, etc.
Case 5
F3 = Arguments(mArgs)
Case 6
V3 = Arguments(mArgs)
Case 7
F4 = Arguments(mArgs)
Case 8
V4 = Arguments(mArgs)
Case 9
F5 = Arguments(mArgs)
Case 10
V5 = Arguments(mArgs)
End Select
Next mArgs
If Trim(UCase(MODE)) = "UPDATE" Then
QueryStr = "UPDATE " & Tablename & " SET "
' I definitely need a Foxpro type macro substitution to help reduce the following loop!
' Maybe an array like MajP is suggesting!!!
Do While True
If Trim(F1) <> "" Then ' First pair of field/update data
QueryStr = QueryStr & F1
Select Case TypeName(v1)
Case "String"
QueryStr = QueryStr & " = '" & v1 & "';"
Case "Integer", "Double", "Boolean"
QueryStr = QueryStr & " = " & v1 & ";"
Case "Date"
QueryStr = QueryStr & " = #" & v1 & "#;"
End Select
Else
MsgBox "You MUST submit at least one field to update!"
Exit Function
End If
If Trim(F2) <> "" Then ' Second pair of field/update data
' Remove the semicolon from the existing query string first.
mLen = Len(QueryStr)
QueryStr = Left(QueryStr, (mLen - 1))
QueryStr = QueryStr & ", " & F2
Select Case TypeName(V2)
Case "String"
QueryStr = QueryStr & " = '" & V2 & "';"
Case "Integer", "Double", "Boolean"
QueryStr = QueryStr & " = " & V2 & ";"
Case "Date"
QueryStr = QueryStr & " = #" & V2 & "#;"
End Select
Else ' If no more parameters to update, exit loop
Exit Do
End If
If Trim(F3) <> "" Then
' Remove the semicolon from the existing query string first.
mLen = Len(QueryStr)
QueryStr = Left(QueryStr, (mLen - 1))
QueryStr = QueryStr & ", " & F3
Select Case TypeName(V3)
Case "String"
QueryStr = QueryStr & " = '" & V3 & "';"
Case "Integer", "Double", "Boolean"
QueryStr = QueryStr & " = " & V3 & ";"
Case "Date"
QueryStr = QueryStr & " = #" & V3 & "#;"
End Select
Else ' If no more parameters to update, exit loop
Exit Do
End If
If Trim(F4) <> "" Then
' Remove the semicolon from the existing query string first.
mLen = Len(QueryStr)
QueryStr = Left(QueryStr, (mLen - 1))
QueryStr = QueryStr & ", " & F4
Select Case TypeName(V4)
Case "String"
QueryStr = QueryStr & " = '" & V4 & "';"
Case "Integer", "Double", "Boolean"
QueryStr = QueryStr & " = " & V4 & ";"
Case "Date"
QueryStr = QueryStr & " = #" & V4 & "#;"
End Select
Else ' If no more parameters to update, exit loop
Exit Do
End If
If Trim(F5) <> "" Then
' Remove the semicolon from the existing query string first.
mLen = Len(QueryStr)
QueryStr = Left(QueryStr, (mLen - 1))
QueryStr = QueryStr & ", " & F5
Select Case TypeName(V5)
Case "String"
QueryStr = QueryStr & " = '" & V5 & "';"
Case "Integer", "Double", "Boolean"
QueryStr = QueryStr & " = " & V5 & ";"
Case "Date"
QueryStr = QueryStr & " = #" & V5 & "#;"
End Select
Else ' If no more parameters to update, exit loop
Exit Do
End If
Loop
Else
' I'll finish this based on the same technique used above for "UPDATE" once
' I determine how to shorten the code with a equivalent of Foxpro macro substitution
'QueryStr = "INSERT INTO " & TableName & " (" & F1 & ") Values (" & v1
End If
Call OpenConnection
CurrentProject.Connection.Execute (QueryStr), , 129
adoConnection.Close
End Function