What's wrong with my VBA code?

khelza

New Member
Joined
Feb 10, 2015
Messages
23
Hey guys,

Trying to create a macro button that, when pressed, populates empty columns, based on a match between Part_ID in excel sheet called BOM and Part_ID in database table called materials joined with tables manufacturers and vendors

The code is as follows:

<code style="font-family: monospace, monospace; margin: 0px 2px; border: 0px; border-radius: 2px; display: block; font-size: 1em; line-height: 1.42857142857143em; padding: 0px !important; background-color: transparent;">
Code:
Private Sub CommandButton21_Click()</code>
'Create a recordset object.
Dim rec As ADODB.Recordset
Dim my_sql As String

Set rec = New ADODB.Recordset
Set cn = New ADODB.Connection

'connect to your mysql server
ADOExcelSQLServer 

With rec
'Assign the Connection object.
.ActiveConnection = cn

'This is where my Part_ID column starts
Range("L6").Select

'Set Do loop to stop when an empty cell is reached.
'create the connection to mysql db*

Do Until IsEmpty(ActiveCell)
my_sql = "Select manufactures.manufacturer, materials.model_number, vendors.vendor, materials.cost_usd from manufactures, materials, vendors"
my_sql = my_sql & " where materials.manufacturer = manufactures.manufacturer And materials.alternate_vendor = vendor.ID And materials.cw_id = " & ActiveCell

With rec
.Open my_sql, cn, adOpenForwardOnly, adLockReadOnly
End With

If rec.BOF() = False Then
'data fill needs to start from column O6
Range("O" & ActiveCell.Row).CopyFromRecordset rec
End If
rec.Close
'Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop

'Tidy up
.Close

End With

cn.Close 'close connect to db

<code style="font-family: monospace, monospace; margin: 0px 2px; border: 0px; border-radius: 2px; display: block; font-size: 1em; line-height: 1.42857142857143em; padding: 0px !important; background-color: transparent;">End Sub

</code>

It stops at
Code:
[COLOR=#4F4F4F].Open my_sql, cn, adOpenForwardOnly, adLockReadOnly[/COLOR]

And I can't figure out why

Any feedback would be greatly appreciated!

Note: I have another module for the connection which includes:
Code:
Public cn As ADODB.Connection

Sub ADOExcelSQLServer()
[FONT=tahoma].
.
.
End Sub[/FONT]
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Ok so apparently it's because my_sql is defined as a string, but it should be an integer? Not sure how to correct that..
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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