更新MS Access中的日期/时间字段使用VBA

huangapple go评论56阅读模式
英文:

Updating a date/time field in MS Access using VBA

问题

以下是您要翻译的内容:

"I'm trying to update a date/time field when a new file is imported through vba.

It works for the first file, but for subsequent imports I get a type mismatch error.

If I delete the 'FileDate' field and re-insert it, it works for the first time, but after I get the error again.

Dim db As DAO.Database

Dim str_table As String
Dim str_sql As String

Dim dt As Date

str_table = "Items"

dt = CDate(Format(FileDateFromPath(file_path), "MM/DD/YYYY"))

Set db = CurrentDb()

str_sql = "UPDATE [" & str_table & "] SET [FileDate] = #" & dt & "# " & _
    "WHERE [FileDate] Is Null OR [FileDate]='';"

DoCmd.TransferSpreadsheet _
        TransferType:=acImport, _
        SpreadsheetType:=acSpreadsheetTypeExcel12Xml, _
        TableName:=str_table, _
        FileName:=file_path, _
        HasFieldNames:=True ' or False if no headers

db.Execute str_sql, dbFailOnError '<-- error here's

I've tried different formats for the date (DD/MM/YYYY, changed to MM/DD/YYYY), I've included # around the date...

Deleting the field and re-inserting it works, but only for the first time...

Is there something I'm missing?"

英文:

I'm trying to update a date/time field when a new file is imported through vba.

It works for the first file, but for subsequent imports I get a type mismatch error.

If I delete the 'FileDate' field and re-insert it, it works for the first time, but after I get the error again.

    Dim db As DAO.Database
    
    Dim str_table As String
    Dim str_sql As String
    
    Dim dt As Date

    str_table = "Items"

    dt = CDate(Format(FileDateFromPath(file_path), "MM/DD/YYYY"))
    
    Set db = CurrentDb()
    
    str_sql = "UPDATE [" & str_table & "] SET [FileDate] = #" & dt & "# " & _
        "WHERE [FileDate] Is Null OR [FileDate]='';"
   
    DoCmd.TransferSpreadsheet _
            TransferType:=acImport, _
            SpreadsheetType:=acSpreadsheetTypeExcel12Xml, _
            TableName:=str_table, _
            FileName:=file_path, _
            HasFieldNames:=True ' or False if no headers
    
    db.Execute str_sql, dbFailOnError '<-- error here's

I've tried different formats for the date (DD/MM/YYYY, changed to MM/DD/YYYY), I've included # around the date...

Deleting the field and re-inserting it works, but only for the first time...

Is there something I'm missing?

答案1

得分: 1

你的 dt 变量是一个日期,而不是一个字符串。因此,你应用的格式没有效果,当你连接这个变量时,它将使用你的本地日期格式。

在与 SQL 连接变量时,可以使用 Gustav 的 CSql() 函数

另外,日期列不能具有值 '',所以你可以省略它。

应该可以这样工作:

str_sql = "UPDATE [" & str_table & "] SET [FileDate] = " & CSql(dt) & _
         " WHERE [FileDate] Is Null;"
英文:

Your dt variable is a Date, not a String. Therefore the Format you apply has no effect, and when you concat the variable, it will use your local date format.

Use Gustav's CSql() function when concatenating variables with SQL.

Also, a date column can not have the value '', so you can omit that.

str_sql = "UPDATE [" & str_table & "] SET [FileDate] = " & CSql(dt) & _
         " WHERE [FileDate] Is Null;"

should work.

huangapple
  • 本文由 发表于 2023年2月9日 00:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定