Problem with VBA function

RadekLodz

New Member
Joined
May 31, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi everybody,
I have problem with my function. Previously code:
VBA Code:
Public Function Wylicz(addressCell As String, cityCell As String, startDate As String, endDate As String)

Dim address As String
Dim city As String

address = Replace(addressCell, " ", "%")
city = Replace(cityCell, " ", "%")

Dim objRec As ADODB.Recordset
Dim objConn As ADODB.Connection
Dim objCmd As ADODB.Command
Dim ConnectionString As String
Dim cmdString As String

Set objRec = New ADODB.Recordset
Set objConn = New ADODB.Connection
Set objCmd = New ADODB.Command

ConnectionString = "Provider=SQLOLEDB;Data source=Server;Initial catalog=Database;User ID=sa;Password=Password"
objConn.Open ConnectionString

cmdString = "DECLARE @table AS TGroups"

objCmd.CommandText = cmdString
objCmd.Execute , , adExecuteNoRecords
MsgBox ("Declare")

cmdString = "INSERT INTO @table (groupName) VALUES ('Group 1')"
objCmd.CommandText = cmdString
objCmd.Execute , , adExecuteNoRecords
MsgBox ("1 INSERT")

cmdString = "INSERT INTO @table (groupName) VALUES ('Group 2')"
objCmd.CommandText = cmdString
objCmd.Execute , , adExecuteNoRecords
MsgBox ("2 INSERT")

cmdString = "INSERT INTO @table (groupName) VALUES ('Group 3')"
objCmd.CommandText = cmdString
objCmd.Execute , , adExecuteNoRecords
MsgBox ("3 INSERT")

cmdString = "EXEC dbo.SprzedazPoAdresieDostawy @groups = @table, @address = address, @city = city, @startDate = startDate, @endDate = endDate"
MsgBox (cmdString)
Set objRec = objConn.Execute(cmdString)

If objRec.EOF = True Then
    Wylicz = 0
    'MsgBox (0)
Else
    Wylicz = objRec!ACTINDX
    'MsgBox (objRec!ACTINDX)
End If
objConn.Close
End Function

After run this function in Excel sheet, a received error value #ARG!. Only first MsgBox is displayed, and function stop run, without any errors or something else. What is wrong with it?

Best Regards
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Have you set a reference to the ADODB type library in the workbook?
Separately, is this something that you really need to be a function and not a macro subroutine that you run on-demand? I ask because I wonder if there might be an issue about making so many calls to the databse if you're using this function from the worksheet from multiple cells... I might not be problematic, but just a thought.
 
Upvote 0
Yes. I set a reference to ActiveX Data Objects and ActiveX Data Objects Recordset.
An yes, i need a function, because stored procedure get data from db search by address and city. There is a list in Excel.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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