Results 1 to 9 of 9

Thread: Opening many recordsets

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Opening many recordsets

    Hello VbForums
    Please moderrators keep my post here.
    I'm using IntelliSense in text change event (3 textboxes)
    When retreiving data from database, there are of course three recordsets which are open.
    This is causing a problem.
    I get object variable or with block variable not set
    Is there a way to overcome this issue please?
    I'm using Sqlite (VbRichClient as platform)
    This is the IntelliSense Sub I use:
    Code:
    Public Sub IntelliSense(sTextBox As TextBox, sTable As String, sField As String, Optional sDBPass As String)
        
        Dim lLen As Long, StrSql As String
        If bExit = True Or sTextBox = "" Then Exit Sub
        lLen = Len(sTextBox)
    cnn.OpenDB MyDBPath
    
        StrSql = "SELECT * FROM " & sTable & " WHERE " & sField & " LIKE '" & sTextBox & "%'"
        
        Set RS = Nothing
       Set RS = cnn.OpenRecordset(StrSql)
        If RS.EOF And RS.BOF Then Exit Sub
        sTextBox.Text = RS(sField)
        If sTextBox.SelText = "" Then
            sTextBox.SelStart = lLen
        Else
            sTextBox.SelStart = InStr(sTextBox.Text, sTextBox.SelText)
        End If
        sTextBox.SelLength = Len(sTextBox.Text)
    End Sub
    thank you
    Last edited by samer22; Jul 17th, 2018 at 07:51 PM.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Opening many recordsets

    Where are you getting the error and what is it you are trying to do?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: Opening many recordsets

    I'm using IntelliSense in Text1 , 2 and 3
    IntelliSense Text1, "Tbl1", "FirstName"
    IntelliSense Text2, "Tbl1", "LastName"
    IntelliSense Text3, "Tbl1", "MiddleName"

    When retieving data from database:
    Text1.text = RS("FirstName").Value
    The error occurs here:
    object variable or with block variable not set
    Text2.text = RS("LastName").Value
    Text3.text = RS("MiddleName").Value

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Opening many recordsets

    Quote Originally Posted by samer22 View Post
    I'm using IntelliSense in Text1 , 2 and 3
    IntelliSense Text1, "Tbl1", "FirstName"
    IntelliSense Text2, "Tbl1", "LastName"
    IntelliSense Text3, "Tbl1", "MiddleName"

    When retieving data from database:
    Text1.text = RS("FirstName").Value
    The error occurs here:
    object variable or with block variable not set
    Text2.text = RS("LastName").Value
    Text3.text = RS("MiddleName").Value
    That code is not in what you have shown so I do not know in what context it applies.
    Have you hovered over the line with the mouse to see which of those two objects is throwing the error? It could be the textbox or the RS.

    Not really sure where you get multiple recordsets. There is only one shown.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Opening many recordsets

    i guess you are calling this from a keydown or keypress event, but i think the way you are doing it, with the 3 text boxes, you could have mismatched name groups, like the firstname could be from a different record than the last name etc
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: Opening many recordsets

    Have you hovered over the line with the mouse to see which of those two objects is throwing the error? It could be the textbox or the RS.
    The Rs is throwing the error.

    I do not know in what context it applies.
    When searching for a record, I have to open the recordset.(Rs.Open) for searching.
    Then, while populating the textboxes, at the Text change event, there is another Rs.open for auto completion(or IntelliSense ).
    And because the initial Rs is not closed,this is why it is throwing the error.
    I hope you'll understand me

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: Opening many recordsets

    westconn1
    I'm calling from text change event

  8. #8
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    764

    Re: Opening many recordsets

    Make the RecordSet variable, RS, local to the Sub.
    That way, it doesn't matter how many events you trigger, how many times you call this code, you cannot get one invocation nullifying the [shared] variable that another one is trying to use. OK, it shouldn't happen but it's the only way I can think of to get the error you describe.

    Code:
    ' If you're going to use Hungarian Notation, 
    ' differentiate between sTring variables and TextBox Controls. 
    
    Public Sub IntelliSense( txtTextBox As TextBox, sTable As String, sField As String, Optional sDBPass As String)
        Dim RS as RecordSet : Set RS = Nothing     
        
        Dim lLen As Long, sSql As String
        If bExit = True Or txtTextBox.Text = "" Then Exit Sub ' bExit not defined here. 
     Are you using Option Explicit?  
        lLen = Len(txt.sTextBox.Text)
        
        cnn.OpenDB MyDBPath
        sSql = "SELECT * FROM " & sTable & " WHERE " & sField & " LIKE '" & txtTextBox.Text & "%'"
        
        Set RS = cnn.OpenRecordset(sSql)
        If RS.EOF And RS.BOF Then Exit Sub
        txtTextBox.Text = RS(sField)
        If sTextBox.SelText = "" Then
            txtTextBox.SelStart = lLen
        Else
            txtTextBox.SelStart = InStr(txtTextBox.Text, txtTextBox.SelText)
        End If
        txtTextBox.SelLength = Len(txtTextBox.Text)
        
        RS.Close
    End Sub
    You might also want to think about Parameterised Queries to avoid SQL Injection attacks, even in a VB6, desktop application!

    Regards, Phill W.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: Opening many recordsets

    I could overcome the issue by using IntelliSense in KeyUp event instead of change event

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width