在Excel中单击超链接时在access中保存文本

在Excel中单击超链接时在access中保存文本,excel,vba,ms-access,hyperlink,Excel,Vba,Ms Access,Hyperlink,我有一个Excel表格,上面有一个代码,每行的末尾都有一个超链接。 接下来应该发生的事情是:单击此超链接后,“数据已确认”应保存到Access文件中 例如:共有10行,由于当前代码,每一行都会添加一个超链接。如果单击第8行的此超链接,“数据已确认”应添加到第8行的Access文件中(仅第8行!) 多亏了Basodre,我现在有了这段代码,但我想不出一种方法来获取Access中保存的文本。有什么想法吗 Private Sub Worksheet_FollowHyperlink(ByVal Targ

我有一个Excel表格,上面有一个代码,每行的末尾都有一个超链接。 接下来应该发生的事情是:单击此超链接后,“数据已确认”应保存到Access文件中

例如:共有10行,由于当前代码,每一行都会添加一个超链接。如果单击第8行的此超链接,“数据已确认”应添加到第8行的Access文件中(仅第8行!)

多亏了Basodre,我现在有了这段代码,但我想不出一种方法来获取Access中保存的文本。有什么想法吗

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
'Confirm that this is a hyperlink in column 3

If Not Intersect(Target.Range, Columns(3)) Is Nothing Then
    MsgBox SaveData(Target.Range)
End If

End Sub

Private Function SaveData(rng As Range) As Boolean
Debug.Print rng.Address & " has been saved."
SaveData = True
End Function

我有一个用于创建DB对象的类模块。它的名称是
AccessBackEnd

Option Explicit

' ConnectModeEnum
'Private Const adModeRead = 1
'Private Const adModeReadWrite = 3
Private Const adModeShareDenyNone As Long = 16

' adStateEnum
'Const adStateClosed As Long = 0                  'Indicates that the object is closed.
Const adStateOpen As Long = 1                    'Indicates that the object is open.
'Const adStateConnecting As Long = 2              'Indicates that the object is connecting.
'Const adStateExecuting As Long = 4               'Indicates that the object is executing a command.
'Const adStateFetching As Long = 8                'Indicates that the rows of the object are being retrieved.

' CursorTypeEnum
Const adOpenStatic As Long = 3

' LockTypeEnum
Const adLockOptimistic As Long = 3

Private dataSource As Object

Public Property Get Connection() As Object
    If dataSource Is Nothing Then
        Set dataSource = CreateObject("ADODB.Connection")
        
        With dataSource
            .Provider = "Microsoft.ACE.OLEDB.12.0"
            .Mode = adModeShareDenyNone
        End With
    End If
    
    Set Connection = dataSource
End Property

Private localFileName as string
Public Property Get FileName() As String
        FileName = localFileName 
End Property

Public Property Set FileName(newFileName As String) As String
        localFileName  = newFileName 
End Property

Public Sub Connect(ByVal dataBaseName As String)
    Connection.Open "Data Source=" & dataBaseName & ";" 
End Sub

''' Recordset command is used to access table data
Public Function Record(ByVal sqlQuery As String) As Object
    If Not ((Connection.state And adStateOpen) = adStateOpen) Then
        Connect FileName
    End If
    
    Set Record = CreateObject("ADODB.Recordset")
    Record.Open Source:=sqlQuery, ActiveConnection:=Connection, CursorType:=adOpenStatic, LockType:=adLockOptimistic
End Function

Public Sub Dispose()
    If dataSource Is Nothing Then
        Debug.Print "You disposed of nothing..."
    Else
        If (Connection.state And adStateOpen) = adStateOpen Then dataSource.Close
        Set dataSource = Nothing
    End If
End Sub
此示例显示如何从Excel更新访问字段

Dim thisDB As AccessBackEnd
Set thisDB = New AccessBackEnd
thisDB.FileName = "Your DB full path goes here.accdb"
With thisDB.Record("SELECT * FROM yourTable WHERE ID=" & PrimaryKey & ";")
    If Not (.EOF Or .BOF) Then
        .Fields("Your Field to update goes here").Value = rng.Address
        .Update
    End If
End With

thisDB.Dispose

这和你的有什么不同?ADODB方法不起作用吗?我将从上一个线程继续讨论。我认为您最好确定Access中的哪些字段作为主键,并使用这些字段进行更新。根据行号进行更新可能会导致结果不一致。如果有人对Excel工作表进行排序,使该行现在实际位于位置10,该怎么办?如果您想发布数据库模式,我们可以帮助您确定主键。一旦我们做到了这一点,剩下的就应该简单明了了。(旁注,我不知道你是否可以根据行号更新Access。其他人必须插话)。为什么使用Excel作为界面?无法使用Access表行号指定记录,必须使用字段值。上面的代码对我无效。Basodre,每行中都有一个唯一的行ID,因此可以用作主键。