MS SQL Update

moone009

Board Regular
Joined
Jan 26, 2010
Messages
82
Basically whatever column is to the right of my input column I would like to use that value as the value to update in my database.

Before I can even test it to see if it works I keep getting "End if without Block if error" any ideas?

HTML:
Sub SNVlookUP_Server()
    Dim cn As ADODB.Connection
    Dim strSQL As String, i As Integer, ii As Integer
    Dim rst As ADODB.Recordset
    Dim a, b, bb, sA As String, sCol As String, Rng As Range, Rng1 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(1, sCol), Cells(65536, sCol).End(xlUp))
        Set Rng1 = Range(Cells(2, 2), Cells(65536, 2).End(xlUp))
        a = Application.WorksheetFunction.Transpose(Rng.Value)
        bb = Application.WorksheetFunction.Transpose(Rng1.Value)
        b = a
        Set cn = New ADODB.Connection
        cn.Open msConnStringDB
        For i = LBound(a) To UBound(a)
        For ii = LBound(bb) To UBound(bb)
'**********************************************************************************************'
'To change the look up value change it.viItemName

'To change the value that the look up value is referencing change "max(th.TransDate) as MaxDate"
'**********************************************************************************************'

                    strSQL = "Update lalocation " & _
                    "Set propertydatamap = '" & VBA.Trim$(bb(ii)) & "'" & _
                    "WHERE ServiceNumber ='" & VBA.Trim$(a(i)) & "'"
                    Set rst = cn.Execute(strSQL)
                
Next
        Rng.Offset(0, 1).Value = Application.WorksheetFunction.Transpose(b)
        cn.Close
        Set cn = Nothing
End If

End Sub
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,792
Office Version
  1. 365
Platform
  1. Windows
You have two Fors but only one Next. Is that single Next intended to terminate both For loops?

If so, change it to Next ii, i.
 
Last edited:

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows
You need to add a next, and you should probably include the loop control variables not just Next on it's own - it get's lonely and doesn't know who to talk to.:)
Code:
        For i = LBound(a) To UBound(a)
            For ii = LBound(bb) To UBound(bb)
                '**********************************************************************************************'
                'To change the look up value change it.viItemName
                'To change the value that the look up value is referencing change "max(th.TransDate) as MaxDate"
                '**********************************************************************************************'
                strSQL = "Update lalocation " & _
                         "Set propertydatamap = '" & VBA.Trim$(bb(ii)) & "'" & _
                         "WHERE ServiceNumber ='" & VBA.Trim$(a(i)) & "'"
                Set rst = cn.Execute(strSQL)
            Next ii
        Next i
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,951
Messages
5,834,540
Members
430,295
Latest member
amdis

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
Top