We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,408 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

choose to save record either in the two database name

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.

3
Contributors
7
Replies
5 Days
Discussion Span
4 Months Ago
Last Updated
8
Views
Question
Answered
vonnoy
Light Poster
31 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

rishif2
Junior Poster
156 posts since Dec 2012
Reputation Points: 54
Solved Threads: 32
Skill Endorsements: 3

thanks a lot bro. it helps a lot.

vonnoy
Light Poster
31 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

its my pleasure
please mark this post as solved

rishif2
Junior Poster
156 posts since Dec 2012
Reputation Points: 54
Solved Threads: 32
Skill Endorsements: 3

is this applicable if im using module?

vonnoy
Light Poster
31 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
rishif2
Junior Poster
156 posts since Dec 2012
Reputation Points: 54
Solved Threads: 32
Skill Endorsements: 3

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

Nice code rishif2...

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

ok thanks rishif2.

vonnoy
Light Poster
31 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 4 Months Ago by rishif2 and AndreRet

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0777 seconds using 2.67MB