Witam wszystkich,
Zrobiłem następującą konstrukcję. Wpierw utworzyłem formularz z kontrolkami. Formularz wywołuję przy pomocy funkcji wpisanej w arkusz Excela. Do przycisku dodałem następującą procedurę:
Private Sub CommandButton1_Click()
Dim address As String
Dim city As String
Dim addressCell As String
Dim cityCell As String
Dim startDate As String
Dim endDate As String
'Dim groups As String
Dim sDate As Date
Dim eDate As Date
Dim objRec As ADODB.Recordset
Dim objConn As ADODB.Connection
Dim objCmd As ADODB.Command
Dim ConnectionString As String
Dim cmdString As String
Dim destCol As Integer
Set objRec = New ADODB.Recordset
Set objConn = New ADODB.Connection
Set objCmd = New ADODB.Command
startDate = TextBox1.Text
endDate = TextBox2.Text
destCol = Range(TextBox3.Text & 1).Column
MsgBox (destCol)
Count = CInt(TextBox4.Text)
sDate = CDate(startDate)
eDate = CDate(endDate)
ConnectionString = "Provider=SQLOLEDB;Data source=Server;Initial catalog=Database;User ID=sa;Password=Password"
objConn.Open ConnectionString
objRec.ActiveConnection = objConn
For i = 4 To Count
addressCell = Cells(i, 4)
cityCell = Cells(i, 5)
address = Replace(addressCell, " ", "%")
city = Replace(cityCell, " ", "%")
'MsgBox (address)
cmdString = "SET NOCOUNT ON;"
cmdString = cmdString & "DECLARE @table AS TGroups" + Chr(13)
cmdString = cmdString & "INSERT INTO @table (groupName) VALUES ('Grupa1')" + Chr(10) + Chr(13)
cmdString = cmdString & "INSERT INTO @table (groupName) VALUES ('Grupa2')" + Chr(10) + Chr(13)
cmdString = cmdString & "INSERT INTO @table (groupName) VALUES ('Grupa3')" + Chr(10) + Chr(13)
cmdString = cmdString & "INSERT INTO @table (groupName) VALUES ('Grupa4')" + Chr(10) + Chr(13)
cmdString = cmdString & "EXEC dbo.SprzedazPoAdresieDostawy @groups = @table, @address = '" & address & "', @city = '" & city & "', @startDate = '" & CStr(sDate) & "', @endDate = '" & CStr(eDate) & "'" + Chr(10) + Chr(13)
cmdString = cmdString & "SET NOCOUNT OFF;"
objRec.Open cmdString
If objRec.EOF = True Then
Cells(i, destCol) = 0
Else
Cells(i, destCol) = objRec.Fields(0).Value
End If
objRec.Close
Next
objConn.Close
End Sub
Jeśli uruchomię program bezpośrednio z konsoli VBA, wszystko jest w porządku. Wpiszę daty, docelowa kolumna i ilość wierszy i naciskam przycisk. Iteracje ładnie przechodzą. Natomiast gdy wywołam to z funkcji. Formularz się odpala, wpisuję dane wejściowe i po naciśnięciu przycisku iteracja zatrzymuje się wewnątrz warunku if. Nawet nie wykonuje tego polecenia w if.
O co może chodzić?