1

我有一张大致如下所示的表格:

损失表

1300 和 850 代表检查这些光纤电缆的频率

真正的问题是 1300 和 850 不是设定值。在不同的文件中,它们可能是“100”和“320”,所以我不能只寻找“850”或“1300”来分隔条目。

有几件事我可以确定:

  • 总是有 2 个不同的频率(从现在开始我们称它们为“A”和“B”)
  • “A”条目的数量总是与“B”条目的数量相同
  • 字符串始终是\<A>\<A>nm_<LocationName>_<CoreNumber>.SOR

我想要的是 2 个单独的表,1 个用于所有“A”条目,一个用于“B”条目。

我怎样才能做到这一点?

我是否必须使用 SQL 或 VBA 都没关系


编辑:

通过在互联网上环顾四周,我大致了解了我希望它如何工作:

  • 将表作为记录集打开。
  • 在每一行中搜索 \'s 之间的值。例子: \<value>\
  • 对于 \ \ 之间的每个新值
  • 用所有具有第一个值的条目填充第一个表(在我们的示例中为 1300)

我只是不知道如何将其翻译成代码,知道如何做到这一点的人,简单点

4

2 回答 2

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
于 2013-07-19T11:04:02.857 回答
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
于 2013-07-16T16:47:52.343 回答