2011-01-13 57 views
1

我有一个带有exp和imp列的ms-access 2003表。在这些exp和imp列中,我有75个国家。我想在同一个表中创建虚拟变量exp1-exp75,imp1-imp75,显示哪个国家是exp以及哪个国家是imp。例如,如果exp是澳大利亚(澳大利亚是第一个国家),那么exp1必须是1,所有其他exp2-exp75应该是0.如果imp是英国(UK是第五国),imp5应该是1,其他所有其他小鬼的应该是0,所以这个表应该是这样的(如果美国是第三和意大利是17国)用0和1填充几列(创建虚拟变量)

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 

感谢。

+2

对于这样一个可怕的桌子设计,你有什么属世理性吗?我假设你必须有一个.... – RolandTumble 2011-01-13 19:01:29

回答

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