I'm not understanding why this script is not working. Basically I combined the two scripts and then created a user form with a few checkboxes......checkbox 1 do this, checkbox 2 do this. But now I am getting a Next without For error. I tried modifying it with the for i next but no help!
Any ideas??
Private Const msConnStringDB = "Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=Portal__Prod;Data Source=RPLSQL"
Private Sub cmdBtn_Click()
Dim cn As ADODB.Connection
Dim strSQL As String, i As Integer, ii As Integer
Dim rst As ADODB.Recordset
Dim a, b, sA As String, sCol As String, Rng As Range, InputBox As String
'**********************************************************************************************'
'sCol is where the user will enter the column of the look up value
'**********************************************************************************************'
'' InputBox = Application.InputBox("Insert DB Connections:")
sCol = Application.InputBox("Please input the column of the look up value:")
If VBA.Len(sCol) = 0 Then
MsgBox "No column is selected!"
Else
Set Rng = Range(Cells(2, sCol), Cells(65536, sCol).End(xlUp))
a = Application.WorksheetFunction.Transpose(Rng.Value)
b = a
Set cn = New ADODB.Connection
cn.Open msConnStringDB
For i = LBound(a) To UBound(a)
'**********************************************************************************************'
'To change the look up value change it.viItemName
'**********************************************************************************************'
If CheckBox1.Enabled = True Then
strSQL = "SELECT lo.address1, lo.locationid " & _
"FROM LATransHeader th " & _
"INNER JOIN LATransDetail td ON th.TransHeaderID = td.TransHeaderID " & _
"INNER JOIN LAItem it ON td.ItemID = it.ItemID " & _
"INNER JOIN LAStop st ON td.StopID = st.StopID " & _
"INNER JOIN LALocation lo ON st.LocationID = lo.LocationID " & _
"INNER JOIN LAStatus sa ON td.StatusID = sa.StatusID " & _
"WHERE lo.address1 ='" & VBA.Trim$(a(i)) & "'"
Set rst = cn.Execute(strSQL)
If Not rst.EOF Then
b(i) = rst.Fields(1).Value
Else
b(i) = ""
End If
Next
Rng.Offset(0, 1).Value = Application.WorksheetFunction.Transpose(b)
cn.Close
Set cn = Nothing
End If
If CheckBox2.Enabled = True Then
strSQL = "SELECT lo.address1, lo.Servicenumber " & _
"FROM LATransHeader th " & _
"INNER JOIN LATransDetail td ON th.TransHeaderID = td.TransHeaderID " & _
"INNER JOIN LAItem it ON td.ItemID = it.ItemID " & _
"INNER JOIN LAStop st ON td.StopID = st.StopID " & _
"INNER JOIN LALocation lo ON st.LocationID = lo.LocationID " & _
"INNER JOIN LAStatus sa ON td.StatusID = sa.StatusID " & _
"WHERE lo.address1 ='" & VBA.Trim$(a(i)) & "'"
Set rst = cn.Execute(strSQL)
If Not rst.EOF Then
b(i) = rst.Fields(1).Value
Else
b(i) = ""
End If
Next
Rng.Offset(0, 1).Value = Application.WorksheetFunction.Transpose(b)
cn.Close
Set cn = Nothing
End If
End Sub
Any ideas??
Private Const msConnStringDB = "Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=Portal__Prod;Data Source=RPLSQL"
Private Sub cmdBtn_Click()
Dim cn As ADODB.Connection
Dim strSQL As String, i As Integer, ii As Integer
Dim rst As ADODB.Recordset
Dim a, b, sA As String, sCol As String, Rng As Range, InputBox As String
'**********************************************************************************************'
'sCol is where the user will enter the column of the look up value
'**********************************************************************************************'
'' InputBox = Application.InputBox("Insert DB Connections:")
sCol = Application.InputBox("Please input the column of the look up value:")
If VBA.Len(sCol) = 0 Then
MsgBox "No column is selected!"
Else
Set Rng = Range(Cells(2, sCol), Cells(65536, sCol).End(xlUp))
a = Application.WorksheetFunction.Transpose(Rng.Value)
b = a
Set cn = New ADODB.Connection
cn.Open msConnStringDB
For i = LBound(a) To UBound(a)
'**********************************************************************************************'
'To change the look up value change it.viItemName
'**********************************************************************************************'
If CheckBox1.Enabled = True Then
strSQL = "SELECT lo.address1, lo.locationid " & _
"FROM LATransHeader th " & _
"INNER JOIN LATransDetail td ON th.TransHeaderID = td.TransHeaderID " & _
"INNER JOIN LAItem it ON td.ItemID = it.ItemID " & _
"INNER JOIN LAStop st ON td.StopID = st.StopID " & _
"INNER JOIN LALocation lo ON st.LocationID = lo.LocationID " & _
"INNER JOIN LAStatus sa ON td.StatusID = sa.StatusID " & _
"WHERE lo.address1 ='" & VBA.Trim$(a(i)) & "'"
Set rst = cn.Execute(strSQL)
If Not rst.EOF Then
b(i) = rst.Fields(1).Value
Else
b(i) = ""
End If
Next
Rng.Offset(0, 1).Value = Application.WorksheetFunction.Transpose(b)
cn.Close
Set cn = Nothing
End If
If CheckBox2.Enabled = True Then
strSQL = "SELECT lo.address1, lo.Servicenumber " & _
"FROM LATransHeader th " & _
"INNER JOIN LATransDetail td ON th.TransHeaderID = td.TransHeaderID " & _
"INNER JOIN LAItem it ON td.ItemID = it.ItemID " & _
"INNER JOIN LAStop st ON td.StopID = st.StopID " & _
"INNER JOIN LALocation lo ON st.LocationID = lo.LocationID " & _
"INNER JOIN LAStatus sa ON td.StatusID = sa.StatusID " & _
"WHERE lo.address1 ='" & VBA.Trim$(a(i)) & "'"
Set rst = cn.Execute(strSQL)
If Not rst.EOF Then
b(i) = rst.Fields(1).Value
Else
b(i) = ""
End If
Next
Rng.Offset(0, 1).Value = Application.WorksheetFunction.Transpose(b)
cn.Close
Set cn = Nothing
End If
End Sub