我正在从excel更新SQL Server中表中的数据,更新数据后,我正在excel中获取更新的数据,当我获取更新的数据时,有时会显示更新的记录,并且有时需要2或3次单击刷新按钮,然后显示更新的数据,我不知道自己在做什么错。
请帮助我解决问题。
代码:
Public Sub UpdateRequest()
Dim ID As Variant, cn As Variant, rs As Variant, RequestID As Variant
Dim StartRow As Long, EndRow As Long, i As Long, PeopleID As Long, Assoc As Long, SysStatus As Long, PortfolioID As Long
Dim strSQL As String, Status As String
Dim dateTime As Date
On Error Resume Next
With Application
.EnableEvents = False
.ScreenUpdating = False
.DisplayAlerts = False
End With
Set Data = Worksheets("Data")
With Data
ID = .Range(.Cells(StartRow, 1), .Cells(EndRow, EndCol)).Value2
.Cells.ClearContents
End With
For i = 1 To UBound(ID)
If ID(i, 1) <> "" And ID(1, 1) <> "RequestID" Then
RequestID = ID(i, 1)
PortfolioID = ID(i, 3)
Status = ID(i, 20)
If Status = "Entry" Then
SysStatus = 1
ElseIf Status = "EntryTransaction" Then
SysStatus = 2
ElseIf Status = "QC" Then
SysStatus = 3
ElseIf Status = "Complete" Then
SysStatus = 4
Else: Status = "Entry"
SysStatus = 1
End If
'#1 Send Log
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Provider = "sqloledb"
cn.Open = "Server=ServerName;Initial Catalog=DataBaseName;Integrated Security=SSPI;"
Set rs.ActiveConnection = cn
strSQL = "SELECT * FROM TableName w WHERE w.RequestID = ('" & RequestID & "')"
rs.Open strSQL, cn, 1, 3
With rs
If rs.RecordCount = 0 Then
.AddNew
!RequestID = RequestID
Assoc = StatusCheck(RequestID, 0, SysStatus, PortfolioID)
Else
Assoc = StatusCheck(RequestID, 1, SysStatus, PortfolioID)
End If
!SysStatus = SysStatus
!DataProcessingAssoc = Assoc
!DataProcessingNotes = ID(i, 21)
!SysAction = 6
!SysTime = dateTime
!ApplicationID = 4
!ApplicationVersion = 1.1
.Update
End With
'Close connections
rs.Close
cn.Close
End If
Next i
'Here in this module fetching the data from sql server and pasting the data in excel
Call Request_Manager
With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
End With
On Error GoTo 0
End Sub