2011-01-13 8 views
1

Ich habe eine MS-Access 2003-Tabelle mit Exp und Imp-Spalten. In diesen exp und imp Spalten habe ich 75 Länder. Ich möchte Dummy-Variablen exp1-exp75, imp1-imp75, in der gleichen Tabelle erstellen, die zeigt, welches Land exp ist und welches Land imp ist. Wenn zum Beispiel exp Australien ist (Australien ist das 1. Land), dann muss exp1 1 sein und alle anderen exp2-exp75 sollten 0. Und wenn imp UK ist (UK ist das 5. Land), sollte imp5 1 und alle anderen sein imp der sollte 0, so dass die Tabelle wie folgt aussehen sollte (wenn USA die dritte und Italien ist das 17. Land)Füllen mehrerer Spalten mit 0 und 1 (Dummy-Variablen erstellen)

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 

Dank.

+2

Welchen irdischen Grund haben Sie für solch ein schreckliches Tischdesign? Ich nehme an, du musst einen haben .... – RolandTumble

Antwort

0

Ich schrieb dies im Access 2007-Editor, aber die VBA sollte identisch sein. Ich glaube nicht, dass Sie das mit Anfragen tun können.

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 
Verwandte Themen