2011-01-13 8 views
1

私は、expとimpの列を持つms-access 2003テーブルを持っています。これらのexpとimpの列には私は75カ国を持っています。同じテーブルにexp1-exp75、imp1-imp75というダミー変数を作成して、どの国がexpか、どの国がimpかを示したいと思います。たとえば、expがオーストラリア(オーストラリアは第1国)ならばexp1は1でなければならず、他のexp2-exp75はすべて0でなければならない。そしてimpが英国(英国は5番目の国)であればimp5は1でなければならず、 IMPさんは(米国が第三で、イタリアは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 

おかげ0でなければなりません。

+2

このような恐ろしいテーブルデザインには、どのような理由がありますか?私はあなたが1つを持っている必要があると仮定しています.... – RolandTumble

答えて

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 
関連する問題