使用0和1填充多个列(创建虚拟变量)

时间:2011-01-13 09:48:21

标签: ms-access ms-access-2003

我有一个带有exp和imp列的ms-access 2003表。在这些exp和imp列中,我有75个国家/地区。我想在同一个表中创建虚拟变量exp1-exp75,imp1-imp75,显示哪个国家/地区是exp,哪个国家/地区是imp。因此,例如,如果exp是澳大利亚(澳大利亚是第一个国家),那么exp1必须是1而所有其他exp2-exp75应该是0.如果imp是UK(UK是第5个国家),imp5应该是1而所有其他imp应该是0.所以表格应该是这样的(如果USA是第3个,意大利是第17个国家)

exp           imp    exp1 exp2 ...exp17 ... exp75 imp1 imp2 imp3 ... imp5 ... imp75


Australia     UK      1    0        0         0     0    0    0        1        0


Italy         USA     0    0        1         0     0    0    1        0        0

感谢。

1 个答案:

答案 0 :(得分:0)

我在Access 2007编辑器中写了这个,但VBA应该是相同的。我不认为你可以用查询来做。

Private Sub FillTable()

Const firstCountry  As Integer = 1
Const lastCountry   As Integer = 75
Const maxRows       As Integer = 234 'or whatever you need...


Dim impCountry  As Integer
Dim expCountry  As Integer

Dim db  As DAO.Database
Dim rs  As DAO.Recordset
Dim i   As Integer

Dim j   As Integer

    'function Random1to75 is left as an exercise for the reader
    impCountry = Random1to75
    expCountry = Random1to75

    Do Until expCountry <> impCountry
        expCountry = Random1to75
    Loop

    Set db = CurrentDb()
    Set rs = db.OpenRecordset("select * from YourTable", dbOpenDynaset)

    For j = 1 To maxRows
        rs.AddNew
            For i = firstCountry To lastCountry
                If i <> impCountry Then
                    rs("imp" & i) = 0
                Else
                    rs("imp" & i) = 1
                End If

                If i <> expCountry Then
                    rs("exp" & i) = 0
                Else
                    rs("exp" & i) = 1
                End If
            Next
        rs.Update
    Next

    rs.Close
    Set rs = Nothing
    Set db = Nothing

End Sub