Hi, I am new to VB6. Currently I am suppose to write a programme for checking book stock.whenever if there is any insufficient books in the data i will have to gather all the insufficient name of any book in a msg and send out to the user. For my current project i tried using array to access mircosoft access 2003 database.I could not check all the element in the array list.

EG.
Name of books:
Harry Portter
Spiderman
Batman

When any of the books is low on stock it will be insufficient. I want to use array to gather the books that is being insufficent so that i could get the information for that book/books.

Dani AI

Generated

Echoing and , common causes for “not checking all elements” in VB6 are: using ReDim without Preserve (which clears previous contents), mixing 0-based vs 1-based indexing (Option Base or UBound/LBound misuse), failing to increment your index, or not advancing the recordset (missing rs.MoveNext). A simple, robust pattern is to collect low-stock titles in a Collection (no ReDim headaches), then build the message string — this avoids most array-bound mistakes and works cleanly with ADO/Access.

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim lowList As New Collection
Dim msg As String
Dim qty As Long

Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Path\Books.mdb;"

Set rs = New ADODB.Recordset
rs.Open "SELECT Title, QtyOnHand FROM Books", cn, adOpenForwardOnly, adLockReadOnly

Do While Not rs.EOF
    If IsNull(rs!QtyOnHand) Then
        qty = 0
    Else
        qty = CLng(rs!QtyOnHand)
    End If

    If qty < 1 Then lowList.Add CStr(rs!Title)
    rs.MoveNext
Loop

If lowList.Count > 0 Then
    Dim i As Long
    For i = 1 To lowList.Count
        msg = msg & lowList(i) & vbCrLf
    Next
    MsgBox "Insufficient stock for:" & vbCrLf & msg
Else
    MsgBox "All books in stock."
End If

rs.Close
cn.Close

Notes: if an array is preferred, initialize it before first use and append with ReDim Preserve arr(0 To n) while tracking an index; remember ReDim without Preserve erases prior entries and ReDim Preserve only keeps the last dimension. For performance push filtering into SQL (e.g., WHERE QtyOnHand < 1) so VB only processes relevant rows.

Recommended Answers

All 2 Replies

What do you mean can you paste your code?

Kindly post your code . What are you doing exactly and what are the problems that you are facing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.