【问题标题】:filling several columns with 0 and 1 (creating dummy variables)用 0 和 1 填充几列(创建虚拟变量)
【发布时间】:2011-01-13 09:48:21
【问题描述】:

我有一个带有 exp 和 imp 列的 ms-access 2003 表。在这些 exp 和 imp 列中,我有 75 个国家/地区。我想在同一张表中创建虚拟变量 exp1-exp75、imp1-imp75,显示哪个国家是 exp,哪个国家是 imp。例如,如果 exp 是澳大利亚(澳大利亚是第一个国家),那么 exp1 必须是 1,所有其他 exp2-exp75 应该是 0。如果 imp 是英国(英国是第 5 个国家),imp5 应该是 1,所有其他imp's 应该是 0。所以表格应该是这样的(如果美国是第 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

谢谢。

【问题讨论】:

  • 你对这种可怕的桌子设计有什么世俗的理由?我假设你必须有一个....

标签: ms-access ms-access-2003


【解决方案1】:

我是在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 2023-03-27
    • 1970-01-01
    • 2019-08-06
    • 2023-01-27
    相关资源
    最近更新 更多