ClaraBarton
Registered User.
- Local time
- Yesterday, 23:49
- Joined
- Oct 14, 2019
- Messages
- 661
After checking an imported table, if a column is missing, I need to add it to the temporary table before it can be imported to the main table. The ID (not the autonumber main ID but a second one) is built using a recordset but if the column is missing it breaks. Occasionally a user will omit it when importing and it is required. I can't add the column to the subform because the table is in use so I'm using this function written by arnelgp:
But... I lack her skills. I want to add to the sql ALTER TABLE... ADD COLUMN before the zzQuerySubForm is created.
Where and how would I do that?
Code:
Public Function AddColumn()
Dim I As Integer
Dim sql As String
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim sFilter As String
Dim tfFiltered As Boolean
Dim tfOrderBy As Boolean
Dim sOrderBy As String
Dim v As Variant
Dim tmp As String
Dim sNewOrder As String
Set db = CurrentDb
On Error Resume Next
If Me.lstSelected.ListCount <> 0 Then 'anything in the Selected box
tfFiltered = Me.Child0.Form.FilterOn 'then subform filter in tfFiltered
tfOrderBy = Me.Child0.Form.OrderByOn 'and put subform orderby in tfOrderby
If tfFiltered Then
sFilter = Me.Child0.Form.Filter '? why?
End If
If tfOrderBy Then '? why?
sOrderBy = Me.Child0.Form.OrderBy
End If
Me.Child0.Form.OrderBy = "" 'then remove order from subform
sql = "SELECT " 'rebuild subform
For I = 0 To Me.lstSelected.ListCount - 1 'from selected box
sql = sql & "[" & Me.lstSelected.Column(1, I) & "], "
Next
sql = Left$(sql, Len(sql) - 2) & " FROM [" & Me.lstTable & "];" 'and the table
Me.Child0.SourceObject = "" 'remove the whole subtable
DoCmd.DeleteObject acQuery, "zzQuerySubForm" 'and the query
Set qd = db.CreateQueryDef("zzQuerySubForm", sql) 'put new query into qd
db.QueryDefs.Append qd 'append to the queries
Set qd = Nothing 'done with qd
db.QueryDefs.Refresh
Application.RefreshDatabaseWindow
Me.Child0.SourceObject = "Query.zzQuerySubForm" 'refill the subform
With Me.Child0.Form
If tfFiltered Then 'put back the filter
.Filter = sFilter
.FilterOn = True
End If
If tfOrderBy Then 'and the orderby
sOrderBy = Replace$(sOrderBy, "[zzQuerySubForm].", "")
v = Split(sOrderBy, ",")
For I = 0 To UBound(v)
tmp = ""
If InStr(1, v(I), " desc") <> 0 Then
tmp = " desc"
v(I) = Trim$(Replace$(v(I), " desc", ""))
End If
If InStr(1, sql, v(I)) <> 0 Then
sNewOrder = sNewOrder & v(I) & tmp & ","
End If
Next
If Len(sNewOrder) <> 0 Then
sNewOrder = Left$(sNewOrder, Len(sNewOrder) - 1)
.OrderBy = sNewOrder
.OrderByOn = True
Else
.OrderByOn = False
End If
Else
.OrderBy = False
End If
End With
Else
Me.Child0.SourceObject = ""
Me!txtSQL = Null
End If
Set db = Nothing
Call SQLString
End Function
But... I lack her skills. I want to add to the sql ALTER TABLE... ADD COLUMN before the zzQuerySubForm is created.
Where and how would I do that?