vb- databases and recordsets

Status
Not open for further replies.
I was browsing a vb database book in barnes and noble to see if i could get any ideas on how to finish a program.
the program works except that the way it prints uses the entire database, and not just the records I've queried for printing.
I saw something where they took the database connection and put the data into a recordset.
how would I go about doing that.
I want to use the same sql string used to view the data (no problems with strings, tested every possible combination of choices for errors), and i want the "recordset" to only be used for printing, it can't affect data input, which is tightly integrated with viewing data.
I'm using ADO with an access database I believe. vb is 6.0 sp5 with a add-in called tdbgrid of something similar (allows for grid viewing of databases in vb)

anyone have any good websites I can hit, or any code suggestions. a website with detailed database info would be best, something I can use for a future resource
 

Coleman

Ars Legatus Legionis
16,790
Subscriptor
Well, by default if you execute a SQL statement against an open ADO connection, the return is a ADO Recordset object.
i.e.
code:<HR>
Code:
Set rs = openConnection.Execute(SQLString)
rs gets a recordset. Further, since you aren't opening a recordset object explicitly, the returned recordset object will be a forward only cursor, with a static locktype, which will prevent you from making changes to the underlying data.
-J
 
I'm using a data environment form to get the data for printing. do I need to do something special in it to get data from a seperate printing only "database" ????
that's probably the main question, if I can do that, I bet the rest is unimportant.

this code is slightly old. I fixed a querying bug on my other maching.

code:<HR>
Code:
 <P>Private Sub Command3_Click()
'Get button<P>Dim strsql As String
Dim Combotext1 As String
Dim combotext2 As String
Dim combotext3 As String
Dim combotext4 As String<P>
strsql = ""
Combotext1 = ""
combotext2 = ""
combotext3 = ""
combotext4 = ""<P>Data1.Refresh<P>'number of sessions choice menu'
If Combo1.Text = "1" Then Combotext1 = "1"
If Combo1.Text = "2" Then Combotext1 = "2"
If Combo1.Text = "3" Then Combotext1 = "3"
If Combo1.Text = "4" Then Combotext1 = "4"
If Combo1.Text = "5" Then Combotext1 = "5"
If Combo1.Text = "6" Then Combotext1 = "6"<P>'full or half menu
If Combo2.Text = "1st Half" Then combotext2 = "1"
If Combo2.Text = "2nd Half" Then combotext2 = "2"
If Combo2.Text = "Full" Then combotext2 = "3"<P>'time tab
If Combo3.Text = "Open" Then combotext3 = "1"
If Combo3.Text = "8:30" Then combotext3 = "830"
If Combo3.Text = "9:30" Then combotext3 = "930"
If Combo3.Text = "2:00" Then combotext3 = "200"
If Combo3.Text = "3:00" Then combotext3 = "300"<P>If Combo1.Text = "All" Or Combo2.Text = "All" Or Combo3.Text = "All" Or Combo4.Text = "All" Then<P>
   If Combo1.Text = "All" Then
        session = ""
   Else
        If Combo2.Text = "All" Then session = "Names.session = " & Combo1.Text & " "
        If Combo2.Text <> "All" Or Text1.Text <> "" Or Text2.Text <> "" Then session = "Names.session = " & Combo1.Text & " AND "
   End If
   
   
    If Combo2.Text = "All" Then
        sessionhalf = ""
    Else
        If Combo3.Text = "All" Then sessionhalf = "Names.sessionHalf = " & combotext2 & " "
        If Combo3.Text <> "All" Or Text1.Text <> "" Or Text2.Text <> "" Then sessionhalf = "Names.sessionHalf = " & combotext2 & " AND "
    End If<P>    If Combo3.Text = "All" Then
        time = ""
    Else
        If Combo4.Text = "All" Then time = "Names.Time = " & combotext3 & " "
        If Combo4.Text <> "All" Or Text1.Text <> "" Or Text2.Text <> "" Then time = "Names.Time = " & combotext3 & " AND "
    End If
    
    If Combo4.Text = "All" Then
       badge = ""
        
    Else
        If Text1.Text = "" Then badge = "Names.Badge = '" & Combo4.Text & "' "
        If Text1.Text <> "" Or Text2.Text <> "" Then badge = "Names.Badge = '" & Combo4.Text & "' AND "
    End If<P>    If Text1.Text = "" Then
        troops = ""
    Else
        If Combo1.Text = "All" And Combo2.Text = "All" And Combo3.Text = "All" And Combo4.Text = "All" Then
            If Text2.Text = "" Then troops = " WHERE Names.troop = '" & Text1.Text & "' "
            If Text2.Text <> "" Then troops = " WHERE Names.troop = '" & Text1.Text & "' AND "
        Else
            If Text2.Text = "" Then troops = "Names.troop = '" & Text1.Text & "' "
            If Text2.Text <> "" Then troops = "Names.troop = '" & Text1.Text & "' AND "
        End If
    End If
    
    If Text2.Text = "" Then
        namels = ""
    Else
        If Combo1.Text = "All" And Combo2.Text = "All" And Combo3.Text = "All" And Combo4.Text = "All" Then
            If Text1.Text = "" Then
            namels = " WHERE Names.LastName = '" & Text2.Text & "' "
            Else
            namels = "Names.LastName = '" & Text2.Text & "' "
            End If
        Else
        namels = "Names.LastName = '" & Text2.Text & "' "
        End If
    End If
    
    <P>If Combo1.Text = "All" And Combo2.Text = "All" And Combo3.Text = "All" And Combo4.Text = "All" Then
    strsql = "SELECT * FROM [Names]" & troops & namels & "ORDER BY [Troop], [LastName], [FirstName]"
Else
    strsql = "SELECT * FROM [Names] WHERE " & session & sessionhalf & time & badge & troops & namels & "ORDER BY [Troop], [LastName], [FirstName]"
End If<P>Data1.RecordSource = strsql
Data1.Refresh
TDBGrid1.Refresh
TDBGrid1.AllowAddNew = False<P>'Update status bar
StatusBar1.SimpleText = " Number of records: " & Data1.Recordset.RecordCount & " | Badge: " & Combo4.Text & ""<P>
If Combo4.Text <> "All" Then
IntMBData (Combo4.ItemData(Combo4.ListIndex))
End If
Exit Sub<P>Else<P>    If Text1.Text = "" Then
        troops = ""
    Else
        If Text2.Text = "" Then troops = " [Troop] = '" & Text1.Text & "' "
        If Text2.Text <> "" Then troops = " [Troop] = '" & Text1.Text & "' AND "
    End If
    
    If Text2.Text = "" Then
        If Text1.Text = "" Then
        namels = ""
        Else
        namels = "AND "
        End If
    Else
        namels = " [LastName] = '" & Text2.Text & "' AND "
    End If<P>strsql = "SELECT * FROM [Names] WHERE" & troops & namels & "[Session] = " & Combo1.Text & " AND [SessionHalf] = " & combotext2 & " AND Names.Badge = '" & Combo4.Text & "' AND [Time] = " & combotext3 & " ORDER BY [Troop], [LastName], [FirstName]"<P>Data1.RecordSource = strsql
Data1.Refresh
TDBGrid1.Refresh
TDBGrid1.AllowAddNew = True

entering data works fine. some fields are auto entered from the chosen field, the same fields used to query data. querying and entering is majorly intertwined.

what I want to do is create an additional place for the sql query to go, say data2, and use it for printing only. it also should to refresh upon every new query, not retaining the previous data.
 
Status
Not open for further replies.