我几乎从来没有寻求帮助,只能靠自己解决这个问题,但是现在我陷入了困境。我只是想将数据从多个表追加到一个表。源表是每个美国州的数据集,每个州的附加查询均相同,只是要从每个州表中提取嵌套的选择脚本。因此,我想创建一个VBA脚本,为每个状态引用一个较小的脚本,而不是为每个状态引用整个追加脚本。我不确定是否应该执行SELECT CASE或FOR TO NEXT或FOR EACH NEXT或DO LOOP或其他操作。
这是我到目前为止的内容:
tblLicenses是一个表,其中包含LicenseState字段,我可以从中提取状态列表。
Function StateScripts()
Dim rst As DAO.Recordset
Dim qryState As String
Dim StateCode As String
Set rst = CurrentDb.OpenRecordset("SELECT LicenseState FROM tblLicenses GROUP BY LicenseState;")
' and I've tried these, but they don't work
' qryState = DLookup("LicenseState", "tblLicenses")
' qryState = "SELECT LicenseState INTO Temp FROM tblLicenses GROUP BY LicenseState;"
' DoCmd.RunSQL qryState
Select Case qryState
Case "CT"
StateCode = "CT"
StateScripts = " SELECT [LICENSE NO] AS StateLicense, [EXPIRATION DATE] AS dateexpired FROM CT "
Case "AK"
StateCode = "AK"
StateScripts = " SELECT [LICENSE] AS StateLicense, [EXPIRATION] AS dateexpired FROM AK "
Case "KS"
StateCode = "KS"
StateScripts = " SELECT [LicenseNum] AS StateLicense, [ExpDate] AS dateexpired FROM KS "
End Select
CurrentDb.Execute " INSERT INTO TEST ( StLicense, OldExpDate, NewExpDate ) " _
& " SELECT State.StateLicense as StLicense, DateExpire AS OldExpDate, State.dateexpired AS NewExpDate " _
& " FROM ( " & StateScripts & " ) AS State " _
& " RIGHT JOIN tblLicenses ON (State.StateLicense = tblLicenses.LicenseNum) " _
& " GROUP BY State.StateLicense, DateExpire, State.dateexpired " _
& " HAVING (((LicenseNum) Like '*" & StateCode & "*') ; "
End Function
答案 0 :(得分:0)
听起来您正在处理使用不同列名的相同信息的输入源,并且您正在努力将其全部合并到一个表中。我假设您正在处理50个经常更新的文本文件。
这是您进行此项目的一种方法...
使用VBA构建文件名的集合(在特定文件夹中使用Dir()
)。然后遍历文件名的集合,执行以下操作:
TableDef
对象中的列,并将变量设置为列的实际名称。 (请参见下面的示例代码)下面是一些有关如何实现此目标的示例代码:
Public Sub Example()
Dim dbs As Database
Dim tdf As TableDef
Dim fld As Field
Dim strLic As String
Dim strExp As String
Dim strSQL As String
Set dbs = CurrentDb
Set tdf = dbs.TableDefs("tblLinked")
' Look up field names
For Each fld In tdf.Fields
Select Case fld.Name
Case "LICENSE", "LICENSE NO", "License Num"
strLic = fld.Name
Case "EXPIRATION", "EXPIRATION DATE", "EXP"
strExp = fld.Name
End Select
Next fld
If strLic = "" Or strExp = "" Then
MsgBox "Could not find field"
Stop
Else
' Build SQL to import data
strSQL = "insert into tblCurrent ([State], [License],[Expiration]) " & _
"select [State], [" & strLic & "], [" & strExp & "] from tblLinked"
dbs.Execute strSQL, dbFailOnError
End If
End Sub
现在,使用包含所有新数据的新表,您可以构建更复杂的分组查询以产生最终输出。我喜欢这种方法,因为我更喜欢在视觉生成器中而不是在VBA代码中管理更复杂的查询。
答案 1 :(得分:0)
感谢您的输入。我想到了您的一个想法:
我创建了表(“ tblStateScripts”),其中的rs!(字段)包含各个列名
Dim rs As DAO.Recordset
Dim DB As Database
Set DB = CurrentDb
Set rs = DB.OpenRecordset("tblStateScripts")
If Not rs.EOF Then
Do
CurrentDb.Execute " INSERT INTO TEST ( StLicense, OldExpDate, NewExpDate ) " _
& " SELECT State.StateLicense as StLicense, DateExpire AS OldExpDate, State.dateexpired AS NewExpDate " _
& " FROM ( SELECT " & rs!FldLicenseState & " AS StateLicense, " & rs!FldExpDate & " AS DateExp " & " FROM " & rs!TblState " _
& " RIGHT JOIN tblLicenses ON (State.StateLicense = tblLicenses.VetLicense) " _
& " GROUP BY State.StateLicense, DateExpire, State.dateexpired " _
& " HAVING (((LicenseNum) Like '*" & rs!StateCode & "*') ; "
rs.MoveNext
Loop Until rs.EOF
End If
rs.Close
Set rs = Nothing