
Originally Posted by
Welshgasman
@Moke123
I was keen to see this, and downloaded it?
However when I tried it out, just on the Querydef, it missed the first field?
What have I done wrong?
You did nothing wrong. ( edit: if you also select the PK field in the list , it should work as is)That's what I was up to 1am last night fixing. When I originally posted that in one of the forums it wasn't done yet but I posted it for some reason or another. It got lost and never finished. I think that has to go back at least 3-4 years ago, if not longer. Colin was probably Ridders at that time.
Not sure what I was thinking at the time, but as I said I used a crappy work-around. I didn't know how to identify the primary key field of a table with vba, so I iterated through the array of fields 1 to UBound instead of 0 to UBound. I always have my PK's as the first field. Last night I started fixing that issue and so far so good.
Here's an example of a change I made last night which seems to work good so far.
Code:
Public Function fUpdateSetString(strIN As String, strTable As String) As String
Dim varString As Variant
Dim i As Integer
Dim strOut As String
Dim iCount As Integer
Dim Pk As String
Pk = fncPrimaryKey(strTable)
varString = Split(strIN, ",")
For i = 0 To UBound(varString)
If varString(i) <> Pk Then
strOut = strOut & Space(10) & Chr(34) & varString(i) & " = p" & iCount & "," & Chr(34) & " & _ " & vbNewLine
iCount = iCount + 1
End If
Next i
If strOut <> "" Then
strOut = Left(strOut, Len(strOut) - 11) & Chr(34) & " & _ "
End If
strOut = strOut & vbNewLine & Space(10) & Chr(34) & " Where " & Pk & " = " & "p" & iCount & Chr(34)
fUpdateSetString = strOut
End Function
This puts the PK field in the right place in the where clause whether you select it in the listbox or not.
Code:
Const Sql_Update As String = _
"Update tblPeople Set " & _
"FirstName = p0," & _
"MiddleName = p1," & _
"LastName = p2," & _
"Suffix = p3" & _
" Where PartyID = p4"
With CurrentDb.CreateQueryDef("", Sql_Update)
.Parameters(0) = Me.FirstName
.Parameters(1) = Me.MiddleName
.Parameters(2) = Me.LastName
.Parameters(3) = Me.Suffix
.Parameters(4) = Me.PartyID
.Execute dbFailOnError
.Close
End With
Another change I made is when you generate the textboxes they are bound to the field rather than unbound.
Lots of changes to come. I'll post a copy after a few more tweaks and updates if you'd like.