如何使用以下字段更正create db:
Dim F As String
Dim S As Long
Dim T As Integer
Dim C As Double //Currency
Dim D As String //Date
F = "ABC"
S = 88869045
T = 9
C = 30.4493 // currency, but can be double
D = "06.08.2010" // date (how to correct convert to date?) //#1
DBTable.Name = "TestTable"
DBTable.Columns.Append "First", adVarWChar, 40
DBTable.Columns.Append 1, adBigInt, 20
DBTable.Columns.Append 0, adInteger
DBTable.Columns.Append 0, adCurrency
DBTable.Columns.Append 0, adDate
DBCatalog.Tables.Append DBTable
,DBCatalog as ADOX.Catalog
和DBTable as ADOX.Table
。
另请参阅#1
处的其他问题。
谢谢!
答案 0 :(得分:2)
如果您的Access版本为2000或更高版本,则可以使用Replace()
功能将短划线替换为日期字符串中的点。然后CDate()
函数将能够将字符串转换为日期/时间值。
Debug.Print CDate(Replace("06.08.2010",".","-"))
6/8/2010
这是我系统上立即窗口的示例,该窗口使用US作为其区域设置。因此CDate()
可以接受包含mm-dd-yyyy格式的日期的字符串。如果你的语言环境不同,你需要投入更多的工作来获得你需要的东西。
答案 1 :(得分:1)
一切正常:
//create database
DBTable.Name = "TestTable"
DBTable.Columns.Append "First", adVarWChar, 40
DBTable.Columns.Append "Second", adDouble, 40
DBTable.Columns.Append "Third", adInteger
DBTable.Columns.Append "Forth", adCurrency
DBTable.Columns.Append "Fifth", adDate
但是关于转换字符串到目前为止的问题仍然存在:
Dim D as String //must be date
D = "06.10.2010"
如何将其转换为日期并在插入到db之后? (类型adDate
和日期是否兼容?)
所以,显示代码的最后一个版本:
// after this: insert data into datatable (not in question, but part of creation db (usage is necessary))
Dim F As String
Dim S As Double
Dim T As Integer
Dim C As Double
Dim D As Date
Dim SS As String
Dim TS As String
Dim CS As String
Dim DS As String
F = "'" + "ABC ABD ACS" + "'" //use of single (') quote
S = 88869045
T = 9
C = 30.4493
D = CDate("20/12/2010") // for any data type! don't forget about it
SS = "'" + Str(S) + "'"
TS = "'" + Str(T) + "'"
CS = "'" + Str(C) + "'"
DS = "'" + Str(D) + "'"
DBConnection.Execute "INSERT INTO TestTable VALUES (" + F + "," + SS + "," + TS + "," + CS + "," + DS + " ) "
P.S。它工作正常,感谢您的评论和答案。