Hi guys,

I'm a little bit confused to declare a certain variable.
I defined it under a button.It's in a do-while loop and it stores calculated values while other this are being executed.

And i wanted to extract that value using another button.I know i can use a function but is there a way that i can define the variable using a certain data type that stores the current value of that variable?

Here's the code anyway

cmd = New OleDbCommand("SELECT [sub_pay_item_quantity].[quantity],[sub_pay_item_unit_rate].[rate] FROM " &
                                   "[sub_pay_item_quantity],[sub_pay_item_unit_rate] WHERE [sub_pay_item_quantity].[sub item]=[sub_pay_item_unit_rate].[sub item] AND " &
                                   "[sub_pay_item_quantity].[sub item]='" & subItem & "' AND [sub_pay_item_quantity].[bridge type]='" & bridgeType & "' " &
                                   "AND [sub_pay_item_quantity].[span]='" & span & "'", conn)

            cmd1 = New OleDbCommand("SELECT * FROM sub_pay_item_quantity WHERE [sub item]='" & subItem & "' AND [bridge type]='" & bridgeType & "'" & _
                                    "AND [span]='" & span & "'", conn)

            data_reader = cmd.ExecuteReader()
            data_reader1 = cmd1.ExecuteReader()

            If data_reader.HasRows = True Then
                Do While data_reader.Read()
                    quantity = CDbl(data_reader.Item("quantity"))
                    rate = CDbl(data_reader.Item("rate"))
                    amount = (quantity * rate)
                    result += amount
                Loop
                total = result
            Else
                MsgBox("Unit rate does not exist", vbCritical, "Bridge Construction Cost Estimate")
                Exit Sub
            End If
            If data_reader1.HasRows = True Then
                Do While data_reader1.Read()
                    payItem = CDbl(data_reader1.Item("pay item"))
                    subpayItem = CDbl(data_reader1.Item("sub pay item"))
                    unit = data_reader1.Item("unit")
                Loop
            End If

:"total" is the name of the variable.

If you declare it at class level(outside any subs or functions), then any routine can access it.

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.