如何更改 ADO 记录集字段的数据类型?
How to change the data type of an ADO recordset field?
在 VBA 中,我有一组 ADO 记录集,我想在用户使用表单时对其进行计算并将记录集中的值存储在内存中。
我的问题:在我对整数进行计算后,我得到 double/float。当我尝试将其存储在记录集中时,它是 cast/changed 到一个整数。我希望保留计算后 double/float 的精度。
我的想法:改变我正在计算和存储的字段的数据类型。 我不知道该怎么做。我该怎么做?我可以这样做吗?
例如:
Option Explicit
Sub test()
Dim rs As ADODB.Recordset
Dim sql As String
sql = "select * from test;"
Set rs = frsGetPgRecordset(sql)
Do While rs.EOF = False
Debug.Print rs!x & "<- original"
Debug.Print Application.WorksheetFunction.Log(rs!x) & "<- what I want stored"
rs.Fields("x") = Application.WorksheetFunction.Log(rs!x)
Debug.Print rs!x & "<- what I get"
rs.MoveNext
Loop
End Sub
我得到:
8<- original
0.903089986991944<- what I want stored
1<- what I get
20<- original
1.30102999566398<- what I want stored
1<- what I get
请注意,这不会更改数据库,因为在 frsGetPgRecordset 中(不提供,因为它对问题不重要)我有:
rs.CursorType = adOpenKeyset
rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic
您可以在此处获得有关该部分的更多信息,因为人们会问:
https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/cursortype-property-ado
Update recordset without updating database
我只想将计算结果存储在新字段中,而不是尝试更改现有字段:
rs.Fields.Append "New_Field", adDouble
在 VBA 中,我有一组 ADO 记录集,我想在用户使用表单时对其进行计算并将记录集中的值存储在内存中。
我的问题:在我对整数进行计算后,我得到 double/float。当我尝试将其存储在记录集中时,它是 cast/changed 到一个整数。我希望保留计算后 double/float 的精度。
我的想法:改变我正在计算和存储的字段的数据类型。 我不知道该怎么做。我该怎么做?我可以这样做吗?
例如:
Option Explicit
Sub test()
Dim rs As ADODB.Recordset
Dim sql As String
sql = "select * from test;"
Set rs = frsGetPgRecordset(sql)
Do While rs.EOF = False
Debug.Print rs!x & "<- original"
Debug.Print Application.WorksheetFunction.Log(rs!x) & "<- what I want stored"
rs.Fields("x") = Application.WorksheetFunction.Log(rs!x)
Debug.Print rs!x & "<- what I get"
rs.MoveNext
Loop
End Sub
我得到:
8<- original
0.903089986991944<- what I want stored
1<- what I get
20<- original
1.30102999566398<- what I want stored
1<- what I get
请注意,这不会更改数据库,因为在 frsGetPgRecordset 中(不提供,因为它对问题不重要)我有:
rs.CursorType = adOpenKeyset
rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic
您可以在此处获得有关该部分的更多信息,因为人们会问: https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/cursortype-property-ado Update recordset without updating database
我只想将计算结果存储在新字段中,而不是尝试更改现有字段:
rs.Fields.Append "New_Field", adDouble