DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Visual Basic 4 / 5 / 6 (http://www.daniweb.com/forums/forum4.html)
-   -   how to retrieve data (http://www.daniweb.com/forums/thread186358.html)

snta Apr 13th, 2009 5:40 am
how to retrieve data
 
i'm new programmer. can u help me , how to retrieve data from database(access) and display in combo box. the value in combo box will automatic display into text box
example:
dtbs=office.mdb
table name= item
field name=itemID,itemName
itemID=M1; itemName=modem
itemID=M2; itemName=repeater

i want this combo box display all itemID in combo box. if the user choose/click, M1, text box will display 'modem' automatically....
how to do like that???plzz help me.....

cguan_77 Apr 13th, 2009 6:07 am
Re: how to retrieve data
 
check out this link

http://en.allexperts.com/q/Visual-Ba...earch-form.htm

snta Apr 13th, 2009 11:28 pm
Re: how to retrieve data
 
Quote:

Originally Posted by cguan_77 (Post 844997)

this example for text box,but now i using combo box (style drop down list).
how to do that???

debasisdas Apr 14th, 2009 4:10 am
Re: how to retrieve data
 
1. open the recordset only for itemID
2. in a for loop add the items to combobox
3. open another recordset for itemname by passing itemID in where clause at runtime dynamically by selecting the itmes from combobox

snta Apr 14th, 2009 5:40 am
Re: how to retrieve data
 
thank you very much for your help

choudhuryshouvi Apr 14th, 2009 5:52 am
Re: how to retrieve data
 
Quote:

Originally Posted by snta (Post 844980)
i'm new programmer. can u help me , how to retrieve data from database(access) and display in combo box. the value in combo box will automatic display into text box
example:
dtbs=office.mdb
table name= item
field name=itemID,itemName
itemID=M1; itemName=modem
itemID=M2; itemName=repeater

i want this combo box display all itemID in combo box. if the user choose/click, M1, text box will display 'modem' automatically....
how to do like that???plzz help me.....

think thats the complete code for you....take a combo box and a textbox...

set properties :-

combox box : name --> cboItem
style --> 2- dropdown list
textbox : name --> txtname

add this reference in the project --> Microsoft Activex Data Object <version no.> Library.......the version no. depends on the version of ms office u have installed.....take the highest one for better performance....

here we go...
Option Explicit

Dim gcn As New ADODB.Connection

Private Sub cboItem_Click()
Dim rs As New ADODB.Recordset

With rs
    .CursorLocation = adUseClient
    .LockType = adLockOptimistic
    .CursorType = adOpenDynamic
    .Open "select itemname from [item] where itemid='" & cboItem.List(cboItem.ListIndex) & "'", gcn

    txtname.Text = IIf(.RecordCount = 0, "", IIf(IsNull(!itemname), "", !itemname))
End With

If rs.State = adStateOpen Then rs.Close
Set rs = Nothing
End Sub

Private Sub Form_Load()
On Error GoTo err1

gcn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\sample.mdb;" & _
                    "Persist Security Info=False"
gcn.Open

Call PopulateCombo

Exit Sub

err1:
    Err.Clear
    MsgBox "Could not open database.", vbCritical, "Error"
    If gcn.State = adStateOpen Then gcn.Close
    Set gcn = Nothing
End Sub

Private Sub PopulateCombo()
Dim rs As New ADODB.Recordset

rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.CursorType = adOpenDynamic
rs.Open "select itemid from [item] order by itemid", gcn

With cboItem
    .Clear
    .AddItem "Select an Item Id"
    .ListIndex = 0
    If rs.RecordCount > 0 Then
        rs.MoveFirst
        While Not rs.EOF()
            .AddItem rs!itemid
            rs.MoveNext
        Wend
        .ListIndex = 1
    End If
End With

If rs.State = adStateOpen Then rs.Close
Set rs = Nothing
End Sub

Private Sub Form_Unload(Cancel As Integer)
If gcn.State = adStateOpen Then gcn.Close
Set gcn = Nothing

End
End Sub

please let me know if this works out for you....for any more questions feel free to ask...

regards
Shouvik

choudhuryshouvi Apr 17th, 2009 5:50 am
Re: how to retrieve data
 
it seems that u have already benefited.....have u got ur answer...

if yes then please mark this thread as SOLVED


All times are GMT -4. The time now is 3:42 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC