i have my two database namely: "data1st.mdb" and "data2nd.mdb", then my combobox items are "1st_semester","2nd_semester". if i chose combobox "1st_semester" then all values inputed from the txtbox will directly save to "data1st.mdb", then 2nd_semester combo save to data2nd.mdb.i got no idea with this. thanks a lot.
please help me with a sample code. i it this week. please.

Recommended Answers

All 7 Replies

i assume that you are using MS Access as Backend and also not using 2007 or higher

And Now , to do so you need to do following :-
first of all add reference for adodb and for that go to project menu and select refernces and select Microsoft ActiveX Data Object 2.6 Library

at general declaration part

Dim con As New ADODB.Connection

now on the form load just fill the combo box so

Private Sub Form_Load()
Combo1.AddItem "semester 1"
Combo1.AddItem "semester 2"
End Sub

and now on the click event of command button

Private Sub Command1_Click()
con.Provider = "Microsoft.Jet.OLEDB.4.0"
If Combo1.Text = "semester 1" Then
con.Open App.Path & "\db1.mdb"
Dim query As String
query = "insert into tbl1 values('" & Text1.Text & "','" & Text2.Text & "')"
con.Execute query, cmdtext
con.Close
Set con = Nothing
ElseIf Combo1.Text = "semester 2" Then
con.Open App.Path & "\db2.mdb"
query = "insert into tbl1 values('" & Text1.Text & "','" & Text2.Text & "')"
con.Execute query, cmdtext
con.Close
Set con = Nothing
Else
MsgBox "Select semester", vbCritical
End If
End Sub

finally dont forget to save you database in your vb project folder as i have used app.path .

hope this helps you

commented: Very well explained. +13

thanks a lot bro. it helps a lot.

its my pleasure
please mark this post as solved

is this applicable if im using module?

code is applicable to module also but you need to make a procedure and call it on click event of the command button

i think you have to do the following :-

remove the code from command button and put it into a module within a procedure of public type.
for example:- i am putting the code in a module (within a procedure namely 'insque') and call it on command button's click event

    Public Sub insque()
    con.Provider = "Microsoft.Jet.OLEDB.4.0"
    If Form1.Combo1.Text = "semester 1" Then
    con.Open App.Path & "\db1.mdb"
    Dim query As String
    query = "insert into tbl1 values('" & Form1.Text1.Text & "','" & Form1.Text2.Text & "')"
    con.Execute query, cmdtext
    con.Close
    Set con = Nothing
    ElseIf Form1.Combo1.Text = "semester 2" Then
    con.Open App.Path & "\db2.mdb"
    query = "insert into tbl1 values('" & Form1.Text1.Text & "','" & Form1.Text2.Text & "')"
    con.Execute query, cmdtext
    con.Close
    Set con = Nothing
    Else
    MsgBox "Select semester", vbCritical
    End If
    End Sub

and on the command button just put the name of the procedure only

Private Sub Command1_Click()
insque
End Sub

and finally also remove general declaration from form1 and put it in module's general declaration with public type scope so you have to write the following:-

Public con As New ADODB.Connection

You have a second post open, this has not been marked as solved. Please do so, thanx.

Nice code rishif2...

ok thanks rishif2.

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.