Error connecting to MySQL using VBA (3709 error)

Matheus Faria

New Member
Joined
Feb 5, 2017
Messages
1
Hi all,
Below is a code that will run every time a particular excel file is opened in order to block unauthorized access.
For this the idea is to check the motherboard serial of the computer and verify if it is registered in a mysql database. If it is not registered it closes the file and otherwise autorize the access.
The Workbook_Open function is responsible for autorize or block the access.
The MBSerialNumber function takes the serial of the motherboard.
The ConnectDB function accesses and performs a search of the MySQL database verifying if the serial of the computer is registered.

At this last function I'm having error 3709 (line 4 from bottom to top). I would like your help to solve this problem.


Code:
 Public cn As Variant
  Private Sub Workbook_Open()
     Call MBSerialNumber
     Call ConnectDB
     If rs < 1 Then
         MsgBox ("Data Security failier, This workbook will close")
         ActiveWorkbook.Save
         ActiveWorkbook.Close
     End If
  End Sub
  Public Function MBSerialNumber() As String
     Dim objs As Object
     Dim obj As Object
     Dim WMI As Object
     Dim sAns As String
     Set WMI = GetObject("WinMgmts:")
     Set objs = WMI.InstancesOf("Win32_BaseBoard")
     For Each obj In objs
         sAns = sAns & obj.SerialNumber
         If sAns < objs.Count Then sAns = sAns & ","
     Next
     MBSerialNumber = sAns
  End Function
  Private Sub ConnectDB()
     Set cn = CreateObject("ADODB.Connection")
     cn.Open "DRIVER={MySQL ODBC 5.3 ANSI Driver};" & _
         "SERVER=xxx.xxx.xx;" & _
         "DATABASE=xxx_xxx;" & _
         "USER=xxxx_xxx;" & _
         "PASSWORD=xxxxxxx"
     Set rs = CreateObject("ADODB.Recordset")
     rs.Open "SELECT COUNT(serial) FROM users WHERE serial='" & MBSerialNumber & "', cn, adOpenStatic, adLockOptimistic"";"
     cn.Close
     Set cn = Nothing
  End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top