所以我可能做得比我能咀嚼的更容易和咬得更多,但我能够使用示例数据库创建适用于我的 MS Access 的东西。我只是通过快速的 Google-fu 完成了所有这些操作,因此它可能不如专家那么优雅。但它有效。这只需要现有表并创建新表,但如果您需要帮助传输数据,那么我可以调整它。
Dim myR As Recordset
Dim strSQL As String
Dim strMOD As String
Dim strFULL As String
Dim strNEW As String
Dim charPOS As Integer
Dim strLEN As Integer
Dim strTABLES() As Variant
Dim dbs As DAO.Database
Dim tdfloop As DAO.TableDef
Dim i As Long
Dim j As Long
Dim strNAME As String
Dim alrEXIST As Boolean
i = 0
Set dbs = CurrentDb
With dbs
For Each tdfloop In .TableDefs
ReDim Preserve strTABLES(0 To i)
strTABLES(UBound(strTABLES)) = tdfloop.Name
i = i + 1
Next tdfloop
End With
Set dbs = Nothing
'select all the rows in your table so we can add them to recordset
strSQL = "SELECT * FROM Files"
'create your recordset
Set myR = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
'now to access each row we use a loop
'if you're not sure the field names, you can access them like this:
'myR.Fields(1).Value
'or if you do know the field name then this
'myR![Filename]
myR.MoveFirst 'This just makes sure you're starting from the first record
Do Until myR.EOF = True
strFULL = myR![FileName] 'set this to string so it can be worked with
strLEN = Len(strFULL) 'gets the length of the string aka filename
strMOD = Right(strFULL, strLEN - 1) 'removes the first \
charPOS = InStr(strMOD, "\") 'gets the positiong of the next \
strNEW = Mid(strMOD, 1, charPOS - 1) 'gets the substring from left to \
'use this to check and see if the name is a table already
For j = 0 To i - 1
If strNEW = strTABLES(j) Then
alrEXIST = True 'boolean created for if table exists
End If
Next
'if not a table, create a table
If alrEXIST = False Then
DoCmd.RunSQL "CREATE TABLE " & strNEW & " ([Field1] text(255), [Field2] text(255))"
End If
alrEXIST = False 'reset value to false
myR.MoveNext 'Move to the next record before restarting the loop
Loop
Set myR = Nothing