Results 1 to 12 of 12

Thread: Problem with retrieving data

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Problem with retrieving data

    i want to create a form which when i enter an ID no, it will view the info about the customer.
    and when i enter an ID which is not in the database, a message box will pop up.
    i've created my database using ms access.
    this is halfway done by me.

    my db + vb file: http://www.mediafire.com/?jaimfyfttez (refer to the form named frmViewInfo)

    Code:
    Private dbcon As New ADODB.Connection
    Private rsCustomer As New ADODB.Recordset
    
    
    Sub openDBConnection()
    
        Dim strconn As String
        
        strconn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                  "Data Source=D:\Documents\Works\MTD2013 - SISTEM PANGKALAN DATA\Laundry System\Laundry System.mdb;" & _
                  "Jet OLEDB:Database Password=;" & _
                  "Jet OLEDB:Engine Type=5;"
                
        dbcon.Open strconn
    
    End Sub
    
    Private Sub Form_Load()
        
        Me.txtICNo.Text = ""
        
        Call openDBConnection
        Call openCustomerRecord
        
    End Sub
    
    Sub openCustomerRecord()
    
        Dim strSQL As String
        
        strSQL = "SELECT * FROM Customer"
        rsCustomer.Open strSQL, dbcon, 3, 1
        
    End Sub
    
    Private Sub cmdEnter_Click()
    
        Me.txtFName.Locked = True
        Me.txtLName.Locked = True
        Me.txtPhoneNo.Locked = True
        Me.txtDSent.Locked = True
        Me.txtDTaken.Locked = True
        Me.txtRegClean.Locked = True
        Me.txtDryClean.Locked = True
        Me.txtTotal.Locked = True
        
        Me.txtFName.Text = rsCustomer!fName
        Me.txtLName.Text = rsCustomer!lName
        Me.txtPhoneNo.Text = rsCustomer!phoneNo
        
    End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Problem with retrieving data

    Welcome to VBForums

    I would recommend taking a look at the "Further Steps" link in my signature (and possibly the "ADO Tutorial" it is based on too), as it explains how to create a search feature.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Re: Problem with retrieving data

    thanks. i'll give it a try first.

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Re: Problem with retrieving data

    I still can't get the correct coding. Actually I want to call data from 3 tables and after the search is done, I want all the chosen data are viewed on text box (which are locked so that they can't be edited).

    I got this error:
    Code:
    Operation is not allowed when the object is closed.
    I did this to my coding:
    vb Code:
    1. Private Sub cmdEnter_Click()
    2.  
    3.     Me.txtFName.Locked = True
    4.     Me.txtLName.Locked = True
    5.     Me.txtPhoneNo.Locked = True
    6.     Me.txtDSent.Locked = True
    7.     Me.txtDTaken.Locked = True
    8.     Me.txtRegClean.Locked = True
    9.     Me.txtDryClean.Locked = True
    10.     Me.txtTotal.Locked = True
    11.    
    12.     Dim strSQL As String
    13.     strSQL = "SELECT * FROM Customer"
    14.    
    15.     If txtICNo.Text <> "" Then
    16.         strSQL = strSQL & " WHERE icNo = " & Val(txtICNo.Text)
    17.     End If
    18.    
    19.     rsCustomer.Close
    20.    
    21.     rsCustomer.Open strSQL, dbcon
    22.            
    23.     Me.txtFName.Text = rsCustomer!fName
    24.     Me.txtLName.Text = rsCustomer!lName
    25.     Me.txtPhoneNo.Text = rsCustomer!phoneNo
    26.    
    27. End Sub

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Problem with retrieving data

    I presume you got that on the rsCustomer.Close line.

    Based on the code you have posted I find it odd that it is closed, as you don't close it anywhere (which is not a good idea, you should close things when you are done with them - especially recordsets and connections, even more so when using Access databases).

    The way I would recommend to correct it is to change that line to this:
    Code:
    If (rsCustomer.State And adStateOpen) = adStateOpen Then rsCustomer.Close
    This will close it only if needed.

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Re: Problem with retrieving data

    As suggested I follow your line given above.
    I got this error.

    Run-time error '3709':
    The connection cannot be used to performed this operation. It is either closed or invalid in this context.

    This is my whole coding:
    vb Code:
    1. Private dbcon As New ADODB.Connection
    2. Private rsCustomer As New ADODB.Recordset
    3.  
    4. Sub openDBConnection()
    5.  
    6.     Dim strconn As String
    7.    
    8.     strconn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    9.               "Data Source=D:\Documents\Works\MTD2013 - SISTEM PANGKALAN DATA\Laundry System\Laundry System.mdb;" & _
    10.               "Jet OLEDB:Database Password=;" & _
    11.               "Jet OLEDB:Engine Type=5;"
    12.            
    13.     dbcon.Open strconn
    14.  
    15. End Sub
    16.  
    17. Private Sub cmdEnter_Click()
    18.  
    19.     Me.txtFName.Locked = True
    20.     Me.txtLName.Locked = True
    21.     Me.txtPhoneNo.Locked = True
    22.     Me.txtDSent.Locked = True
    23.     Me.txtDTaken.Locked = True
    24.     Me.txtRegClean.Locked = True
    25.     Me.txtDryClean.Locked = True
    26.     Me.txtTotal.Locked = True
    27.    
    28.     Dim strSQL As String
    29.     strSQL = "SELECT * FROM Customer"
    30.    
    31.     If txtICNo.Text <> "" Then
    32.         strSQL = strSQL & " WHERE icNo = " & Val(txtICNo.Text)
    33.     End If
    34.  
    35.     If (rsCustomer.State And adStateOpen) = adStateOpen Then rsCustomer.Close
    36.  
    37.     rsCustomer.Open strSQL, dbcon, 3, 1
    38.    
    39.     Me.txtFName.Text = rsCustomer!fName
    40.     Me.txtLName.Text = rsCustomer!lName
    41.     Me.txtPhoneNo.Text = rsCustomer!phoneNo
    42.  
    43. End Sub

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Problem with retrieving data

    You should insert the procedure of opening the connection object first...
    Code:
    Private Sub cmdEnter_Click()
     openDBConnection
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Re: Problem with retrieving data

    ^ Looks like it solved but I'm not sure why it stated that there's Run-Time Error -2147217913(80040e07), it's about data mismatch. I've checked the database, I'm not sure which it refers to.


    The search function above only valid for data which already in the database. I want to put a msgbox which says "No record found" if the data (IC No) is not in the database. But I don't know where to put it.



    To be honest, I am only a beginner in VB6 but I was already asked to do such a difficult system and my lecturer just want me to search for internet help. That's why I have no idea how to make this system works properly...

  9. #9
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Problem with retrieving data

    On what line does the error occurs? I can only guess that it is on Val(txtICNo.Text) where the value of txtICNo. is not a valid number?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Re: Problem with retrieving data

    line 37.

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Problem with retrieving data

    How is icNo declared, is it a number or a text? If it is a text then you should enclose it with quotes...

    Code:
    WHERE icNo = '" & Val(txtICNo.Text) & "'"
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Problem with retrieving data

    While dee-u's suggestion in post #7 will work, it is not an ideal way to do things.. and neither is the way you declared the variables (for info, see the article Why shouldn't I use "Dim .. As New .."? from our Classic VB FAQs).

    To see a better way, I would recommend reading the ADO Tutorial (link in my signature), as it shows how to declare/use those variables in a better way, and does other things you might find useful too - and all of it is explained.


    Note that it is a bad idea to use "magic numbers" as you did on this line:
    Code:
        rsCustomer.Open strSQL, dbcon, 3, 1
    It is better to use the constants provided instead (such as adOpenReadOnly), as it makes the code much easier to read - and thus easier to spot possible problems, and maintain, etc. The constants are part of the ADO library, so if you can use a recordset or connection etc, you can be sure that the constants are available too.

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