Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

union query? 1

Status
Not open for further replies.

Machiaveli

Programmer
Dec 16, 2003
91
NL
Hi,

I have table where i have the following fields in datasheetview:

id | date | image | question1 | question2 | question..N

I would like to have it in this way:

id | date | image | questionnr | answer
1 01-01-2004 test.tif 1 1000 (this is the value of field question1)

As you can see the first 3 fields remains the same, but the records of those 3 fields should be inserted for each record of the question fields. I can do this with a union query for each questionfield, but.....

How can i do this automatically, because there are more than 500 columns.
 
Hi,

Paste this code into a vba module and run...
Code:
Sub UnionQuery()
    sql1 = "select id, date, image,"
    For i = 1 To 500
        If i = 1 Then
            Sql = Sql & sql1 & i & ", question" & i & " From MyTable "
        Else
            Sql = "Union " & Sql & sql1 & i & ", question" & i & " From MyTable "
            Cells(i, 1).Value = "Union "
        End If
        Cells(i, 1).Value = Cells(i, 1).Value & sql1 & i & ", question" & i & " From MyTable "
    Next
    docmd.runsql Sql
End Sub


Skip,
[sub]
[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue][/sub]
 
...

naturally, substitute the correct Table Name.

Skip,
[sub]
[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue][/sub]
 
Hi SkipVought,

Thnx for your help, but i've pasted the code in vba in access, but it can't recognize the cells part of the code. Something about sub or function not defined.

Is it a code for excell?
 
sorry.

Please remove the cells statement. I used that for testing in Excel.

Skip,
[sub]
[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue][/sub]
 
Hi Skip,

I've removed the cells part but i'm still getting an error message.

It seems that the sql is invalid. The errormessage is:

expected delete, select, etc.

 
Sorry, I had the Union missplaced...
Code:
    For i = 1 To 500
        If i = 1 Then
            Sql = Sql & sql1 & i & ", question" & i & " From MyTable "
        Else
            Sql = Sql & "Union " & sql1 & i & ", question" & i & " From MyTable "
        End If
    Next

Skip,
[sub]
[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue][/sub]
 
Hi skip,

I really appreciate the help you've given me.

I've changed the code a little bit, because i forgot something. The question must also be a fieldname and the value to (see code below).

The docmd.runsql still gives me errors about invalid statements. I used debug.print to get some results and i ran it in sql analyzer of sql server and it worked after a few adjustments.

Public Sub Union()

Dim i As Integer
Dim sql As String
Dim sql1 As String

sql1 = "select id, date, time, reference , 'question' as variablename, 'question' as value "
For i = 1 To 501
If i = 1 Then
sql = sql & sql1 & ", question" & i & " From MyTable "
Debug.Print sql
Else
sql = sql & "Union " & sql1 & ", question" & i & " From MyTable "
Debug.Print sql
End If
Next
DoCmd.RunSQL sql, False

End Sub

Can you modify it so it can work for me?
 
Code:
Public Sub Union()

Dim i As Integer
Dim sql As String
Dim sql1 As String

sql1 = "select id, date, time, reference, "
 For i = 1 To 501
        If i = 1 Then
            sql = sql & sql1 & " question" & i & ", " & i & " From MyTable "
            Debug.Print sql
        Else
            sql = sql & "Union " & sql1 & " question" & i & ", " & i & " From MyTable "
            Debug.Print sql
        End If
    Next
DoCmd.RunSQL sql, False

End Sub

Skip,
[sub]
[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top