954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

adding record into mysql database

when i try to add a record

Private Sub cmdDone_Click()
Dim Rs As New ADODB.Recordset
Set Rs = New ADODB.Recordset

Call ServerConnect

Rs.Open "SELECT * FROM list1", conn, adOpenStatic, adLockOptimistic
Rs.AddNew


            Rs!CustomerName = txtName.Text
            Rs!ContactNumber = txtContact.Text
            Rs!Date = dt.Value
            Rs!TimeStart = cmbStart.Text
            Rs!TimeEnd = cmbEnd.Text
            Rs!Event = cmbEvent.Text
            Rs!Guest = txtGuest.Text
            Rs!Comment = txtComment.Text

Rs.Update

Rs.Close
conn.Close
End Sub
Attachments 3.jpg 67.17KB
Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

On which line did the error occur?

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 
Private Sub cmdDone_Click()
Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

Below the cmd_Done, it will highlight the actual line where the object is not defined. I need to see which object it is. It can only be Rs, ServerConnect, conn.

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

this

Attachments 4.jpg 60.16KB 5.jpg 76.61KB
Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

Ahhh, that is because you have two subs with the same name "ServerConnect", both using conn as the connection, hence the ambigitous name detected error. Delete or rename one of the subs.:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

private Call server connect

meenu.ms34
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 1
 

variable not defined

Attachments 4.jpg 60.16KB
Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

No, do not make it private. Change the name of the sub say ServerConnect1 and the other one ServerConnect2. Keep it public and in your module.

When calling the connection, use "Call ServerConnect1" or if you are using 2 "Call ServerConnect2"

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

still the same variable not defined

look at my module is this correct

Public Sub ServerConnect1()

Dim conn As ADODB.Connection
Dim strIPAddress As String

Set conn = New ADODB.Connection
strIPAddress = "localhost"

conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
            & "SERVER=" & strIPAddress & ";" _
            & "DATABASE=database1;" _
            & "UID=root;" _
            & "PWD=;" _
            & "OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384

conn.CursorLocation = adUseClient
conn.Open
End Sub

Public Sub ServerConnect2()

Dim conn As ADODB.Connection
Dim strIPAddress As String

Set conn = New ADODB.Connection
strIPAddress = "localhost"

conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
            & "SERVER=" & strIPAddress & ";" _
            & "DATABASE=database1;" _
            & "UID=root;" _
            & "PWD=;" _
            & "OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384

conn.CursorLocation = adUseClient
conn.Open
End Sub
Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

Your code is perfect. Did you change it in the call statement as well -

'You had...
Call ServerConnect
'Should be....
Call ServerConnect1
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

yes but still variable not defined

Dim Rs As New ADODB.Recordset
Set Rs = New ADODB.Recordset

Call ServerConnect1

Rs.Open "SELECT * FROM list1", conn, adOpenStatic, adLockOptimistic
Rs.AddNew


            Rs!CustomerName = txtName.Text
            Rs!ContactNumber = txtContact.Text
            Rs!Date = dt.Value
            Rs!TimeStart = cmbStart.Text
            Rs!TimeEnd = cmbEnd.Text
            Rs!Event = cmbEvent.Text
            Rs!Guest = txtGuest.Text
            Rs!Comment = txtComment.Text

Rs.Update
Rs.Close
conn.Close
Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

Try the following so we can see where the error occur. It might be that conn is not connecting to your MySql database. Does your MySql require a password?

Dim Rs As New ADODB.Recordset
Set Rs = New ADODB.Recordset
 
'Call ServerConnect1

Dim conn As ADODB.Connection
Dim strIPAddress As String
 
Set conn = New ADODB.Connection
strIPAddress = "localhost"
 
conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
            & "SERVER=" & strIPAddress & ";" _
            & "DATABASE=database1;" _
            & "UID=root;" _
            & "PWD=;" _
            & "OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384
 
conn.CursorLocation = adUseClient
conn.Open
 
Rs.Open "SELECT * FROM list1", conn, adOpenStatic, adLockOptimistic
Rs.AddNew
 
 
            Rs!CustomerName = txtName.Text
            Rs!ContactNumber = txtContact.Text
            Rs!Date = dt.Value
            Rs!TimeStart = cmbStart.Text
            Rs!TimeEnd = cmbEnd.Text
            Rs!Event = cmbEvent.Text
            Rs!Guest = txtGuest.Text
            Rs!Comment = txtComment.Text
 
Rs.Update
Rs.Close
conn.Close
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

it's working :)

Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

can i put this in my module?
then i will use Call ServerConnect

Dim conn As ADODB.Connection
Dim strIPAddress As String
 
Set conn = New ADODB.Connection
strIPAddress = "localhost"
 
conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
            & "SERVER=" & strIPAddress & ";" _
            & "DATABASE=database1;" _
            & "UID=root;" _
            & "PWD=;" _
            & "OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384
 
conn.CursorLocation = adUseClient
conn.Open
Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

There must be another reason then why it is not opening from the module, Very strange indeed. I think we need to move the "Call ServerConnect" to the first line and all other code after that. Try that and see what happens, unless you want to keep it as it is now, working!;)

If you are happy with the solution, don't forget to mark this as solved, thanks.:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

Sorry I missed the second post, yes, that should be fine. Just move the call statement up to the first line of code as per my previous reply.

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: