HI everyone
i am trying to write POS and Inventory for myself in vb6. I am stuck and gone blank what to do. here is what i want to do.
search database for the stocks.
change the caption of the button ( eg caption of the botton is "COKE 250ml" and i want it to change to "COKE 250ml [30]" now [30] is the number of coke in stock.

Private Function FindButtonName()
    Dim strTest As String
    Dim strArray() As String
    Dim intCount As Integer
    Dim strButtonName As String

    Dim rsDiscriptionFind As ADODB.Recordset
    Set rsDiscriptionFind = New ADODB.Recordset
    rsDiscriptionFind.Open "Stocks", gobjConn, adOpenDynamic, adLockPessimistic

    rsDiscriptionFind.Filter = "Discription='" & strIC & "'"

    strTest = strIC
    strArray = Split(strTest, " ")

    For intCount = LBound(strArray) To UBound(strArray)
       Trim (strArray(intCount))
    Next
    If intCount = "1" Then
        strButtonName = "Cmd" & strArray(0)
    ElseIf intCount = "2" Then
        strButtonName = "Cmd" & strArray(0) & strArray(1)
    ElseIf intCount = "3" Then
        strButtonName = "Cmd" & strArray(0) & strArray(1) & strArray(2)
    ElseIf intCount = "4" Then
        strButtonName = "Cmd" & strArray(0) & strArray(1) & strArray(2) & strArray(3)
    Else
        strButtonName = "Cmd" & strArray(0) & strArray(1) & strArray(2) & strArray(3) & strArray(4)
    End If
   ' "FrmGraphicMenu.'" & strButtonName & .Caption&"'" = "test"   'this is not working
End Function

Private Sub Command7_Click()

    InitializeDB
    Dim rsStChk As New ADODB.Recordset
    Set rsStChk = New ADODB.Recordset
    rsStChk.Open "Stocks", gobjConn, adOpenDynamic, adLockPessimistic

    Dim rsSearchStock As ADODB.Recordset
    Set rsSearchStock = New ADODB.Recordset
    rsSearchStock.Open "Stocks", gobjConn, adOpenDynamic, adLockPessimistic
    Do Until rsSearchStock.EOF = True
        strIC = rsSearchStock!Discription
        MsgBox strIC
        Call FindButtonName
        rsSearchStock.MoveNext
    Loop
End Sub

it is in loop and i want all the buttons get their quantities updated after every sale entery.
thank you in advance

Dani AI

Generated

As shown by , iterating the form's Controls and matching the right CommandButton will update captions at runtime; was also right to push the focus onto the event that triggers the update. That approach works, but building control names from product descriptions and relying on string parsing is brittle and leads to subtle bugs (spaces, punctuation, duplicate descriptions, and off‑by‑one/count mistakes).

Common pitfalls seen in the original code: calling Trim(...) without assigning the result back to the array element, comparing an Integer to a string literal, and using the loop counter after the loop to infer the number of words. A more robust pattern is to assign a stable product identifier to each button (use the control's Tag) and create a fast mapping from product ID to the CommandButton at form load. On each sale: (1) update the stock in the database (in a transaction), (2) fetch the new quantity and display name for that product, and (3) update only the matching button's caption.

Example pattern (VB6) using a dictionary for O(1) lookups:

' module-level
Dim btnMap As Object

Private Sub Form_Load()
    Set btnMap = CreateObject("Scripting.Dictionary")
    ' ensure each button.Tag is set to the product ID (design-time or here)
    btnMap.Add CmdCoke.Tag, CmdCoke
    btnMap.Add CmdPepsi.Tag, CmdPepsi
End Sub

Sub UpdateProductCaption(prodID As String, displayName As String, qty As Long)
    If btnMap.Exists(prodID) Then
        Dim btn As CommandButton
        Set btn = btnMap(prodID)
        btn.Caption = displayName & " [" & qty & "]"
        btn.Refresh
    End If
End Sub

Practical tips: use parameterized ADO commands to fetch/update a single stock row instead of opening the whole table, validate quantities (no negatives), refresh only the affected control, and wrap the DB update in a transaction to avoid race conditions in multi-terminal setups. For long-term maintainability, consider migrating to a modern runtime (VB.NET or a web front end) when convenient.

Recommended Answers

All 6 Replies

Save yourself now. You know that VB6 support is long gone and since you can get into the Express versions of Visual Studio, why VB6 today?

As to your update, you code that up and fire it off when the event you want happens. As VB6 events are well done on the web, I stop here.

I am writting in vb6 cause i am little use to it for other ver like .net i will have to go through studies again

I changed from VB6 to later in just a few days. It's not very different here.

That's a lot to plow through to "change the caption of the button." Is the issue the button.caption = "something" or you are still learning about events, etc?

PS. Yes I see your line of code "not working" but very few have VB6 running today so be more specific. Is it VB6 yelping at syntax or something else?

let me put it like this i have 10 button ex.
b1
b2
b3 ..... b10
we know the names of the buttons
for example b1.caption = Coke 250ml
when i make a sale it should chk the database for stock and tell me remaing number of coke 250ml
now when the file is in loop we get the string of name but how to call the function usin value of string. that i cant figure out

thank you so much it worked as a charmed.

dim y as control
for each y in controls
    if typeof y is commandbutton then
        if y.name = strbuttonmame then
            y.caption = stric & " [" & rsdiscriptionfind!Qty & "]"
        end if
    end if
next
commented: Good to read you applied the lead and fixed it. +10
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.