help needed with beginner->intermediate VB6 database programming (i'm in learning process)

Status
Not open for further replies.
:(

hi all
I'm using VB6 and ms access 97 as the database.
Actually I modified these codes from a vb6 books somewhere and make it to suit my need as a learning tools.

Just wondering'¦ is there anything wrong with these lines of codes and functions.
What I'm trying to do is that I wanted to call the Select_Item function in class (Financial.dll). This will populate all the textbox in the clientFrm with the related data, base on the itemNo. And ad the same time will populate the listview (lstView) upon loading of the client form.

The Select_data function below is in a client form (clientFrm.vbp). I called it on form open and on refresh command button_click

Code:
'ClientFrm.vbp

Private Sub Select_Data()
    
        Dim itmNew As ListItem
        
        Set rsFinancial = New ADODB.Recordset
        Set rsFinancial = objFinancial.Select_Item
        
        lstView.ListItems.Clear
        
        If rsFinancial.EOF And rsFinancial.BOF Then
            Set rsFinancial = Nothing
            Exit Sub
        End If
        
        Do Until rsFinancial.EOF
            Set itmNew = lstView.ListItems.Add _
                        (, rsFinancial("itemNo"), rsFinancial("itemNo"))
                itmNew.SubItems(1) = rsFinancial("ItemDesc")
                itmNew.SubItems(2) = rsFinancial("unitPrice")
                itmNew.SubItems(3) = rsFinancial("qtyStock")
                itmNew.SubItems(4) = rsFinancial("Status")
                
            rsFinancial.MoveNext
        Loop
        
        Set rsFinancial = Nothing
                      
        
End Sub


'Financial.cls compiled to financial.dll
'+++++++++
Public Function Select_Item(Optional itemNo As String) As Object
    
    Dim strSQL As String
    
    On Error GoTo BadSelect
    
    strSQL = "SELECT * FROM itemMaster"
    
   If Trim(" " & itemNo) <> " " Then
        strSQL = strSQL & " WHERE itemNo= '" & itemNo & "'"
   End If
    
    Set Select_Item = cnFinancial.Execute(strSQL)
    
    Exit Function
    

BadSelect:

    Debug.Print Err.Number & Err.Description
    Select_Item = Err
    Err.Clear
    
End Function

Is there a problem in the code somewhere? because nothing happens during my form open and during cmd refresh button click. I also noticed that my "itemNo" from the Select_Item function contains null " " when i trace it using the break mode. My table all have data and the ItemNo is the primary key.

2.
this is from the same project in the clientfrm
ListView

Code:
  Private Sub lstView_Click()

    On Error GoTo SelectError:
    
    Set rsFinancial = New ADODB.Recordset
    Set rsFinancial = objFinancial.Select_Item(lstView.SelectedItem.Key)
    
    If rsFinancial.EOF And rsFinancial.BOF Then
        Set rsFinancial = Nothing
        Exit Sub
    End If
    
    txtItemNo = rsFinancial("itemNo")
    txtItemDesc = rsFinancial("ItemDesc")
    txtUPrice = rsFinancial("UnitPrice")
    txtQtyStock = rsFinancial("qtyStock")
    cmbStatus = rsFinancial("Status")
    
    Set rsFinancial = Nothing
    
    Exit Sub
    
SelectError:
    MsgBox Err.Number & Err.Description
    Err.Clear
    
End Sub

Whenever I clicked the listview I got this error message.
"91 Object Variable or With block variable not set"

what is that mean? Is it related to the previous problem as well?

Thank for all the help and will be much appreciated

Kuman.

P/s: if the info is not enough please let me know I really need to finish this and i'm still in the learning process :0)


thanks again
:confused: :(

[This message was edited by kuman on May 13, 2002 at 03:41.]
 
First things first, use Option Explicit.
Second, I don't see where you specifying your database.


Write another driver program just to test database connectivity.
Example,
Code:
dim rs as new ADODB.RecordSet
rs.Open "select * form ItemMaster", "DSN connection string here"

while (not rs.eof)
  debug.print rsFinancial("ItemDesc")
  rs.moveNext
wend
 
hi again...

sorry for the confusion...

i have all the connection and the Option Explicit in the thing... what i posted above is the fraction of the whole thing... do i need to post all of the codes in here?

anyway below is my connection thinggy.. :)

Code:
 Option Explicit
Dim cnFinancial As ADODB.Connection

'************************************
' FUNCTION TO ESTABLISH CONNECTION
'************************************
Private Sub class_initialize()

    Set cnFinancial = New ADODB.Connection
        
       On Error GoTo connect_Fail
     
    With cnFinancial
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = App.Path & "\Financial.mdb"
        .Open
    End With
    

    MsgBox "Connection established", vbOKOnly, "CONFIRM"
         
    Exit Sub
    
connect_Fail:
    MsgBox Err.Description & " " & Err.Number
End Sub


Private Sub class_terminate()
    cnFinancial.Close
    Set cnFinancial = Nothing
    
End Sub

Above is my clsFinancial.cls (class), i compiled it to financial.dll prior to executing the program.. still the same though.. the Select_Item function is also in this class.


from my clientFrm

Code:
 Option Explicit
Dim objFinancial As Financial.clsFinancial
Dim rsFinancial As ADODB.Recordset

'to populate the listview on open
Private Sub Form_Load()
    Set objFinancial = New Financial.clsFinancial
    Select_Data
    
    txtRegDate = Format(Now(), "mmmm dd,yyyy")
End Sub

'to populate the listview upon command
Private Sub cmdRefresh_Click()
    Select_Data
End Sub

the rest all the same as above..

that you hope this help you and all the information is provided.. (i think )

kuman :confused: ;)
 
hi ..

i tried it using oledb for Access 97.. nope.. nada.. still the same..

anyway... you study in Wright State Univ?

anyway..

what i'm trying to do with this code is to populate/retrieve the information of the Item Database (inventory) to the listview in the client form. All the data already in the ms Access 97 table(database) which includes the ItemNo, Item Description,UnitPrice, Quantity, & status (using combo box). ItemMasteris the table name in the database Financial.mdb.

what trouble me is that, why does this line/variable didn't have any value in it (itemNo)?
Code:
    strSQL = strSQL & " WHERE itemNo= '" & itemNo & "'"


I think that what makes this thing didn't work for me.. hmmm it seems like the Select_Item function (in the class) didn't receive any value from the Select_Data Function (the clientFrm).

hmmmmmm

anyway thanks for the respond and help...

kuman
:confused: :rolleyes:
 

Aeirould

Ars Tribunus Angusticlavius
7,535
Subscriptor
Your Trim is blowing it out.

If you pass anything to Select Item without the optional parameter, your statement still tries to find

Where itemNo = ' '

because the Trim(), since it kills spaces, kills the leading space you're appending to force type conversion on itemNo.

Try taking the spaces out between the " " (make it "") and it'll work. Better would be:

Code:
Public Function Select_Item(Optional itemNo As String = "NotThere") As ADODB.Recordset

     Dim strSQL As String
     On Error GoTo BadSelect
     strSQL = "SELECT * FROM itemMaster"
     If itemNo <> "NotThere" Then strSQL = strSQL & " WHERE itemNo= '" & itemNo & "'"

     Set Select_Item = cnFinancial.Execute(strSQL)
     OK = True
     Exit Function

BadSelect:
    Debug.Print Err.Number & Err.Description
    ErrorResponse = Err.Number & Err.Description
    Err.Clear
    OK = False
End Function


Add a Public OK AS Boolean to your header, add a Private ErrorReponse to your header, and a Property called ErrorDescription or something of that like with a Get only.

It's really a bad idea to use the blanket Object type in the manner you've used it here - where it cannot receive the same methods regardless of what object is pulled. VB6 doesn't have polymorphism, but we can at least observe good principle.

This leaves Select_Item = nothing (as a failed object call should), but if you check the OK on the return (before you call it), you'll be fine.
 
Aeirould

hi again... thanks a lot... it does help me A LOT

yup i think the trim what makes it bad... hmmm

It's really a bad idea to use the blanket Object type in the manner you've used it here - where it cannot receive the same methods regardless of what object is pulled. VB6 doesn't have polymorphism, but we can at least observe good principle.

hmm i just wonder, why do some book (the one i'm using now) recommend to do it like that hmm... anyway now i know a new thing that books didn't teach me.. :D

anyway..


ok now i have 1 more problem...with the listview i created..

everything is populated correctly in the listview after the last error but...

what i wanted is for any data selected by me from the listview will populate the corresponding txtbox in the form for example the itemNo -> txtItemNo etc..

here's the coding i used for the lstview_click()

Code:
  Private Sub lstView_Click()

    On Error GoTo SelectError:
    
    Set rsFinancial = New ADODB.Recordset
    Set rsFinancial = objFinancial.Select_Item(lstView.SelectedItem.Key)
    
    If rsFinancial.EOF And rsFinancial.BOF Then
        Set rsFinancial = Nothing
        Exit Sub
    End If
    
    txtItemNo = rsFinancial("itemNo")
    txtItemDesc = rsFinancial("ItemDesc")
    txtUPrice = rsFinancial("UnitPrice")
    txtQtyStock = rsFinancial("qtyStock")
    cmbStatus = rsFinancial("Status")
    
    Set rsFinancial = Nothing
    
    Exit Sub
    
SelectError:
    MsgBox Err.Number & Err.Description
    Err.Clear
    
End Sub

and my listview seem to display everything wrongly example..

my Item No suppose to be under ItemNo caption, but instead it display under the "item Description" caption and the rest just follow it from there. it display in the wrong column..

i use VIEW = lvwReport

thanks again for your earlier solutions and recommendation.

kuman :D :p :confused:[
 
Status
Not open for further replies.