2

如何使用以下字段更正创建数据库:

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.CatalogDBTable as ADOX.Table

另请参阅附加问题#1。谢谢!

4

2 回答 2

2

如果您的 Access 版本是 2000 或更高版本,您可以使用该Replace()函数将日期字符串中的点替换为破折号。然后该CDate()函数将能够将字符串转换为日期/时间值。

Debug.Print CDate(Replace("06.08.2010",".","-"))
6/8/2010 

That was an example from the Immediate window on my system, which uses US as its locale setting. So CDate() can accept a string containing a date in mm-dd-yyyy format. If your locale is different, you will need to put in more work to get what you need.

于 2012-05-06T20:48:08.760 回答
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"

如何将其转换为日期,然后插入数据库?(类型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 + " ) "

PS它工作正常,感谢您的评论和回答。

于 2012-05-06T20:43:11.247 回答