如何更改ADO记录集字段的数据类型?

时间:2022-01-26 16:54:16

In VBA I have a collection of ADO recordsets that I want to do calculations on and store the values in the recordset in memory while the user is using the form.

在VBA中,我有一组ADO记录集,我希望在用户使用表单时对记录集中的值进行计算并将其存储在内存中。

My issues: after I do a calculation on, say, and integer, I get a double/float. When I try to store that in the recordset it is cast/changed to an integer. I want the precision of the double/float after the calculation to be preserved.

我的问题:在我做了一个计算,比方说,和整数之后,我得到了一个double / float。当我尝试将其存储在记录集中时,它被转换/更改为整数。我希望保留计算后双精度/浮点数的精度。

My idea: change the data type of the field I am calculation and storing. I'm not seeing how to do it. How do I do that? Can I do that?

我的想法:更改我正在计算和存储的字段的数据类型。我没看到怎么做。我怎么做?我能这样做吗?

Ex:

例如:

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

I get:

我明白了:

8<- original
0.903089986991944<- what I want stored
1<- what I get
20<- original
1.30102999566398<- what I want stored
1<- what I get

Note, this is not changing the database because in frsGetPgRecordset (not provide because it's not critical to the question) I have:

注意,这不是更改数据库,因为在frsGetPgRecordset中(不提供,因为它对问题不重要)我有:

rs.CursorType = adOpenKeyset  
rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic

You can get more info here on that part, because people will ask: https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/cursortype-property-ado Update recordset without updating database

您可以在这里获得更多信息,因为人们会问:https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/cursortype-property-ado更新记录集而不更新数据库

1 个解决方案

#1


3  

I would just store calculations results in a new field instead of trying to change an existing field:

我只是将计算结果存储在一个新字段中,而不是尝试更改现有字段:

rs.Fields.Append "New_Field", adDouble

#1


3  

I would just store calculations results in a new field instead of trying to change an existing field:

我只是将计算结果存储在一个新字段中,而不是尝试更改现有字段:

rs.Fields.Append "New_Field", adDouble