尝试使用变量作为值vba到sql

编程入门 行业动态 更新时间:2024-10-09 07:22:48
本文介绍了尝试使用变量作为值vba到sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Dim connection As New adodb.connection Dim sql As String Dim years As Integer years = 2011

所以我在这里有一些很好的声明变量。现在我要显示完美的声明,没有问题:

So I've got some nice declared variables here. Now I'm going to show the statement that works perfectly, no problems:

sql = "insert into crimeData(reportYear) values(2011)" connection.Execute sql

注意我在一年硬编码。现在,如果我尝试:

Notice I hardcoded in the year. Now, if I try:

sql = "insert into crimeData(reportYear) values(years)" connection.Execute sql

将变量名替换为值,我得到错误:没有给出一个或多个所需参数的值

Substituting the variable name in as a value, I get the error: No value given for one or more required parameters

诚然,我假设有一个简单的语法问题,或一些元我失踪的变量请启发我如何正确地交换一个变量(所以我可以使用这个编程方式,而不是硬编码的值)

Admittedly, I'm assuming there's got to be a simple syntax issue, or some meta with variables I'm missing. Please enlighten me. How can I correctly swap in a variable here (so I can use this programatically, as opposed to hard coding in values)

推荐答案

如果您对连接SQL有更高的需求,您可以从此功能中获益:

If you have a more demanding need for concatenating SQL, you may benefit from this function:

' 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

更多推荐

尝试使用变量作为值vba到sql

本文发布于:2023-11-28 20:26:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   vba   sql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!