我有一张大致如下的表格:
1300和850表示检查这些光缆的频率
真正的问题是1300和850没有设定值。在另一个文件中,它们可以是“100”和“320”,所以我不能只查找“850”或“1300”以分隔条目。
我可以确定一些事情:
\<A>\<A>nm_<LocationName>_<CoreNumber>.SOR
我想要的是2个单独的表,1个用于所有“A”条目,1个用于“B”条目。
我该怎么做?
如果我必须使用SQL或VBA
,则无关紧要编辑:
通过浏览互联网,我已经了解了我希望如何工作:
\<value>\
我只是不知道如何将其翻译成代码,知道如何做到这一点的人,轻松点
答案 0 :(得分:1)
所以我可能已经制作了比我可以咀嚼更容易和更多的声音,但我能够使用示例数据库创建适用于我的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
答案 1 :(得分:1)
感谢TKEyi60的回答,我被放到了正确的轨道上。不得不在这里和那里调整代码来解决这个问题:
Public Function SplitTable()
Dim SQL As String
Dim strMOD As String
Dim strFULL As String
Dim strNEW As String
Dim charPOS As Integer
Dim strLEN As Integer
Dim i As Long
Dim j As Long
Dim alrEXIST As Boolean
Dim strTABLES() As Variant
Dim Rcst As DAO.Recordset
Dim dbs As DAO.Database
Dim tdfloop As DAO.TableDef
i = 0
Set dbs = CurrentDb
For Each tdfloop In dbs.TableDefs
ReDim Preserve strTABLES(0 To i)
strTABLES(UBound(strTABLES)) = tdfloop.Name
i = i + 1
Next tdfloop
Set dbs = Nothing
'Select all the rows in the table so they can be added to a Recordset
SQL = " SELECT * FROM tblTotaalVerlies"
Set Rcst = CurrentDb.OpenRecordset(SQL, dbOpenDynaset)
Rcst.MoveFirst
Do Until Rcst.EOF = True
strFULL = Rcst![FileName] 'set this to string so it can be worked with
strLEN = Len(strFULL) 'gets the length of the filename
strMOD = Right(strFULL, strLEN - 1) 'removes the first \
charPOS = InStr(strMOD, "\") 'gets the positiong of the next \
strNEW = Mid(strMOD, 1, charPOS - 1)
'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 j
'if not a table, create a table
If alrEXIST = False Then
DoCmd.RunSQL "CREATE TABLE " & strNEW & " ([Filename] varchar(32), [Verlies] varchar(32))"
'Renew tabledef array
i = i + 1
ReDim Preserve strTABLES(0 To i - 1)
strTABLES(UBound(strTABLES)) = strNEW
End If
alrEXIST = False 'reset value to false
Rcst.MoveNext 'Move to the next record before restarting the loop
Loop
Set Rcst = Nothing
End Function