excel sql vba

ahmimas

New Member
Joined
Dec 19, 2020
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
hi,
I wrote an excel vba application with a user form that reads data from a sheet and populate a list.
it was working fine, but when I removed a column in the spreadsheet and changed slightly the design, I start having the following error: please see the image.
there is the spot where the program stalls.
I have exactly the same code in another file and it is working fine.
the sheet name is Products.
Please help.

code is here:


VBA Code:
Option Explicit
Public cnn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public strSQL As String

Private Sub UserForm_Initialize()
Dim lngWinstate As XlWindowState

With Application
.ScreenUpdating = False
lngWinstate = xlMaximized
Me.Move 0, 0, .Width, .Height
.WindowState = lngWinstate
.ScreenUpdating = True
End With

OpenDB
displayData


End Sub

VBA Code:
Public Sub OpenDB()
closeRS
If cnn.State = adStateOpen Then cnn.Close
cnn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
cnn.Open
End Sub
Public Sub closeRS()
If rs.State = adStateOpen Then rs.Close
rs.CursorLocation = adUseClient
End Sub
VBA Code:
Public Sub displayData()

strSQL = "Select Distinct [Code] From [Products$] Order by [Code]"

UserForm1.CmbPlc.Clear
rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
Do While Not rs.EOF
If Not IsNull(rs.Fields(0)) Then
UserForm1.CmbPlc.AddItem rs.Fields(0)

End If

rs.MoveNext
Loop
Else
MsgBox "I was not able to find any unique Products.", vbCritical + vbOKOnly
Exit Sub
End If
End Sub
 

Attachments

  • excel_error.png
    excel_error.png
    6.7 KB · Views: 9

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi
how does your connection string compare to the working file
Usually this issue is cause by an incorrect connection string

Thanks
Excel Fan
 
Upvote 0
Hi
how does your connection string compare to the working file
Usually this issue is cause by an incorrect connection string

Thanks
Excel Fan
thank Efan,
it is exactly the same. I also noticed that if I make a layout change in the working file spreadsheet, I start having the same issue without changing anything to the code.
in this situation, I must close excel and restart it.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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