我一直在困惑这个问题,似乎无法自己解决这个问题。我写了一个Excel工具,可以计算出特定的金额。我必须为10,000种不同的情况执行此操作,并且必须将结果存储在Access中。但是,我无法弄清楚如何。我需要使用的功能看起来像这样:
> Public Function Assets(TG As Integer, t As Integer, n As Integer,
> Tarif As String, j As Integer)
>
> Assets = ...
>
> End Function
在同一个模块中,我有一个结合了一些过程的宏,还应该将结果插入到我的Access数据库中。我在这个数据库中有两列:案例编号和结果的值。
> Sub Results()
>
> Dim n As Integer Dim Tarif As String Dim TG As Integer Dim t As
> Integer Dim i As Integer Dim j As Integer Dim dbfile2 As String Dim
> dbe2 As Object ' As DAO.DBEngine Dim db2 As Object ' As
> DAO.Database Dim rs2 As Object ' AS DAO.Recordset
>
>
> n = ... Tarif = ... TG = ...
>
> For j = 1 To 10000
>
> 'Opens the Access database
> dbfile2 = "\\r74efc11\PR_GDV_TRA\Transparenz_2016_2017\20_TP_F\02_Arbeitspakete\CRK_Berechnung\Beispiel-Hochrechnung\Hochrechnung_Ablaufvermögen_Test.accdb"
> Set dbe2 = CreateObject("DAO.DBEngine.120")
> Set db2 = dbe2.OpenDatabase(dbfile2)
>
> db2.Execute "INSERT INTO Tabelle1 (Nr, [Value])VALUES(" & j & "," & Assets(TG,n,n,j) & ")"
>
> db2.Close
> Set db2 = Nothing
> Set dbe2 = Nothing
>
> Next j
>
> End Sub
此代码在没有Assets-function的情况下工作正常,而Assets-function也在Excel中正确运行。我只想在Access数据库中插入结果。 anyonle可以帮助我吗?我在网上找到的只是如何插入工作表函数,但这不适用于我的情况。我试图修改它,但没有任何运气。
答案 0 :(得分:1)
您不应该替换,而是转换。虽然现在已经解决了,但是将来你可能会受益于这个功能:
' Converts a value of any type to its string representation.
' The function can be concatenated into an SQL expression as is
' without any delimiters or leading/trailing white-space.
'
' Examples:
' SQL = "Select * From TableTest Where [Amount]>" & CSql(12.5) & "And [DueDate]<" & CSql(Date) & ""
' SQL -> Select * From TableTest Where [Amount]> 12.5 And [DueDate]< #2016/01/30 00:00:00#
'
' SQL = "Insert Into TableTest ( [Street] ) Values (" & CSql(" ") & ")"
' SQL -> Insert Into TableTest ( [Street] ) Values ( Null )
'
' Trims text variables for leading/trailing Space and secures single quotes.
' Replaces zero length strings with Null.
' Formats date/time variables as safe string expressions.
' Uses Str to format decimal values to string expressions.
' Returns Null for values that cannot be expressed with a string expression.
'
' 2016-01-30. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function CSql( _
ByVal Value As Variant) _
As String
Const vbLongLong As Integer = 20
Const SqlNull As String = " Null"
Dim Sql As String
Dim LongLong As Integer
#If Win32 Then
LongLong = vbLongLong
#End If
#If Win64 Then
LongLong = VBA.vbLongLong
#End If
Select Case VarType(Value)
Case vbEmpty ' 0 Empty (uninitialized).
Sql = SqlNull
Case vbNull ' 1 Null (no valid data).
Sql = SqlNull
Case vbInteger ' 2 Integer.
Sql = Str(Value)
Case vbLong ' 3 Long integer.
Sql = Str(Value)
Case vbSingle ' 4 Single-precision floating-point number.
Sql = Str(Value)
Case vbDouble ' 5 Double-precision floating-point number.
Sql = Str(Value)
Case vbCurrency ' 6 Currency.
Sql = Str(Value)
Case vbDate ' 7 Date.
Sql = Format(Value, " \#yyyy\/mm\/dd hh\:nn\:ss\#")
Case vbString ' 8 String.
Sql = Replace(Trim(Value), "'", "''")
If Sql = "" Then
Sql = SqlNull
Else
Sql = " '" & Sql & "'"
End If
Case vbObject ' 9 Object.
Sql = SqlNull
Case vbError ' 10 Error.
Sql = SqlNull
Case vbBoolean ' 11 Boolean.
Sql = Str(Abs(Value))
Case vbVariant ' 12 Variant (used only with arrays of variants).
Sql = SqlNull
Case vbDataObject ' 13 A data access object.
Sql = SqlNull
Case vbDecimal ' 14 Decimal.
Sql = Str(Value)
Case vbByte ' 17 Byte.
Sql = Str(Value)
Case LongLong ' 20 LongLong integer (Valid on 64-bit platforms only).
Sql = Str(Value)
Case vbUserDefinedType ' 36 Variants that contain user-defined types.
Sql = SqlNull
Case vbArray ' 8192 Array.
Sql = SqlNull
Case Else ' Should not happen.
Sql = SqlNull
End Select
CSql = Sql & " "
End Function