Question : SQL Union MS Access, filling out combo box...

I think this should be easy - It seems I've done it before, but I'm not sure what I'm doing wrong...  I have the following SQL Statment:

"Select CategoryID, CategoryName from Category where CategoryID = 27 Union Select CategoryID, CategoryName from Category where CategoryActive = 1 and CategoryID <> 27"

I want to return the results with the results from the first query first, and the remaning categories after that (possibling in asc. order)

When i run this query on the Access Database, it just returns all the categories.  I made sure that category 27 does exist, and it shows up in the list.

I'm trying to use this query to fill in a combo box in vb.net, so getting the selected category on top is important.  The combo box fills out using the code below - the only thing, I'm not sure how to set the first item returned as the selected item.

Any help with this would be appreciated!  Thanks!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
Private Sub FillCbo(ByVal theSQL As String)
        Dim dr As DataRow
        Dim dt As DataTable
        Dim cmd As New OleDbCommand
        Dim ds As New DataSet
        Dim da As New OleDb.OleDbDataAdapter

        pCat.Items.Clear()
        sql = theSQL
        'sql = "Select CategoryID, CategoryName from Category Order by CategoryName ASC"
        Try
            With da
                .TableMappings.Add("Table", "Category")
                cmd = New System.Data.OleDb.OleDbCommand(sql, con)
                .SelectCommand = cmd
                .Fill(ds)
                .Dispose()
            End With

            ds.AcceptChanges()
            dt = ds.Tables.Item(0)
            ds.Dispose()

            pCat.Text = ""
            pCat.Items.Clear()
            pCat.BeginUpdate()
            For Each dr In dt.Rows
                pCat.Items.Add(dr("CategoryName").ToString)
            Next
            pCat.EndUpdate()
        Catch ex As Exception
            MsgBox("There was an error accessing Categories: " & vbCrLf & ex.Message, MsgBoxStyle.OkOnly, "Error!")
        End Try

    End Sub

Answer : SQL Union MS Access, filling out combo box...

try this

Select CategoryID, CategoryName,1 as [Sort] from Category where CategoryID = 27
Union Select CategoryID, CategoryName,2 as [Sort] from Category where CategoryActive = -1 and CategoryID <> 27
order by [sort], CategoryName
Random Solutions  
 
programming4us programming4us