【问题标题】:Save text in access when clicking on a hyperlink in Excel单击 Excel 中的超链接时将文本保存在 access 中
【发布时间】:2021-02-02 12:47:15
【问题描述】:

我有一个 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

【问题讨论】:

  • 这与您的previous question 有何不同? ADODB 方法不起作用吗?
  • 我将在我们之前的帖子中跟进。我认为您最好确定 Access 中的哪些字段用作主键,并使用它来进行更新。根据行号进行更新可能会导致结果不一致。如果有人对 Excel 工作表进行排序,使得该行现在实际上位于第 10 位怎么办?如果您想发布数据库模式,我们也许可以帮助确定主键。一旦我们有了它,剩下的就应该很简单了。 (旁注,我不知道您是否可以根据行号更新 Access。其他人必须插话)。
  • 为什么用Excel做界面?不能使用 Access 表行号来指定记录,必须使用字段值。
  • 上面的代码对我不起作用。 Basodre,每行都有一个唯一的行 ID,因此可以用作主键。

标签: excel vba ms-access hyperlink


【解决方案1】:

我有一个用于创建数据库对象的类模块。它的名字是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 更新 Access 字段。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 2021-05-12
    • 2017-07-01
    • 2016-08-03
    • 2014-04-10
    • 2018-09-20
    相关资源
    最近更新 更多