【问题标题】:Set cell value with VBA使用 VBA 设置单元格值
【发布时间】:2016-08-26 11:36:19
【问题描述】:

我在 Microsoft Access 中使用 VBA 使用 Wia 扫描和保存图像。

保存图像的文件路径应设置为当前单元格的值。

我不知道如何做到这一点,但在学习了如何使用 Wia 之后,这似乎是一件容易的事。

这是我当前扫描文档的代码。

Function scanImage() As String
    Dim imagePath As String
    Dim folder As String
    folder = "C:\Users\username\Pictures\scans\"
    Dim tempName, obj
    Set obj = CreateObject("Scripting.FileSystemObject")
    tempName = obj.GetTempName
    Dim filename
    filename = Now
    filename = Replace(filename, ".", "_")
    filename = Replace(filename, " ", "_")
    filename = Replace(filename, ":", "_")
    imagePath = folder & filename & ".jpg"

    Dim dev As Device
    Dim wiaDialog As New WIA.CommonDialog
    Dim wiaImage As WIA.ImageFile
    Set dev = wiaDialog.ShowSelectDevice
    Set wiaImage = wiaDialog.ShowAcquireImage
    wiaImage.SaveFile (imagePath)
    scanImage = imagePath

End Function

【问题讨论】:

  • Access 中没有“单元格”
  • 好吧,我可以在“单元格”的设定值处创建数据库列。如果这是正确的词汇表,我如何访问和操作“数据库条目”? :)
  • 你有很多东西要学——这里写的太多了。你真的应该浏览一下初学者的教程并通过示例代码来完成。

标签: ms-access vba wia


【解决方案1】:

正如 cmets 所说 - Access 中没有单元格,当然也没有活动单元格。 您可以使用以下任一方法将记录添加到数据库,但您打算如何再次提取该信息?

例如,在 Excel 中,您只要求输入单元格 A1 中的数据,但在数据库中,您通常要求来自同一记录上的另一个字段等于其他值的一个或多个字段的数据(通过提供'其他值' 直接或通过引用数据库中的其他表)。

因此,例如,在您的数据库中,您会询问在特定日期扫描的所有文件的文件路径,或者有某种描述字段来识别文件。
可以这样写:
SELECT FilePath FROM Table2 WHERE DescriptionField = 'MyPhoto'

无论如何,将单个文本字符串(图像路径)放入表中的新记录的答案是:

Sub InsertValueToTable()

    Dim imagepath As String

    imagepath = "<file path>"

    'NB:  The table name is 'Table2', the field (column) within the table is called 'FilePath'.

    'One way to do it:
    'DoCmd.RunSQL "INSERT INTO Table2(FilePath) VALUES ('" & imagepath & "')"

    'Another way to do it:
    Dim rst As dao.Recordset
    Set rst = CurrentDb.OpenRecordset("Table2")
    With rst
        .AddNew
        rst!FilePath = imagepath
        .Update
        .Close
    End With

    Set rst = Nothing

End Sub  

注意 - 如果您在数据库中使用 Text 字段,您将被限制为 255 个字符。

【讨论】:

  • 谢谢! DAO 是我需要听到的一切。看起来我用谷歌搜索错了:)
猜你喜欢
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 2020-02-09
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 2022-01-07
相关资源
最近更新 更多