Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ode,而不仅仅是执行SQL所必需的。从你当前的帖子来看,@Brad的答案已经提供了一个解决方案。是的,对不起,我不清楚这一点。我有一个有效的SQL语句,我正试图在VBA源代码中执行此SQL语句,这需要更改格式。我希望将结果返回给我。没有插入/更新/删除更_Sql_Vba_Excel - Fatal编程技术网

ode,而不仅仅是执行SQL所必需的。从你当前的帖子来看,@Brad的答案已经提供了一个解决方案。是的,对不起,我不清楚这一点。我有一个有效的SQL语句,我正试图在VBA源代码中执行此SQL语句,这需要更改格式。我希望将结果返回给我。没有插入/更新/删除更

ode,而不仅仅是执行SQL所必需的。从你当前的帖子来看,@Brad的答案已经提供了一个解决方案。是的,对不起,我不清楚这一点。我有一个有效的SQL语句,我正试图在VBA源代码中执行此SQL语句,这需要更改格式。我希望将结果返回给我。没有插入/更新/删除更,sql,vba,excel,Sql,Vba,Excel,ode,而不仅仅是执行SQL所必需的。从你当前的帖子来看,@Brad的答案已经提供了一个解决方案。是的,对不起,我不清楚这一点。我有一个有效的SQL语句,我正试图在VBA源代码中执行此SQL语句,这需要更改格式。我希望将结果返回给我。没有插入/更新/删除更多的是验证数据是否存在,所以只需选择Hanks,但我的主要问题是将我的SQL Select转换为VBA@JohnJones我更新了以包含返回的记录集Hey Bard-让我更新我的问题,以包含到目前为止的代码。谢谢,但我的主要问题是将我的SQL


ode,而不仅仅是执行SQL所必需的。从你当前的帖子来看,@Brad的答案已经提供了一个解决方案。是的,对不起,我不清楚这一点。我有一个有效的SQL语句,我正试图在VBA源代码中执行此SQL语句,这需要更改格式。我希望将结果返回给我。没有插入/更新/删除更多的是验证数据是否存在,所以只需选择Hanks,但我的主要问题是将我的SQL Select转换为VBA@JohnJones我更新了以包含返回的记录集Hey Bard-让我更新我的问题,以包含到目前为止的代码。谢谢,但我的主要问题是将我的SQL Select转换为VBA,我已经能够通过VBA在数据库中运行查询了,我只是想用正确的VBA表示法将SQL转换为VBA是什么意思?如果你有SQL,就不需要进行转换。查询不是“在VBA中运行”。VBA只是在其他地方方便SQL的执行。SQL就是SQL。很抱歉,可能转换不是正确的词,但是为了在VBA上运行查询,它需要以某种方式格式化。如果您转到“我的链接提供”,您将看到他需要更改字符串以便在VBA上运行您的查询是否包含在一个Excel行中?看起来不像添加了任何strSql=“strSql=”“”&strSql&“”,查看debuge时添加的双引号可能是带有Replace的内容(strSql、vbCrLf、strlineend)?您是否试图获取有效的SQL字符串并将其转换为可执行的“字符串”?或者,您正在尝试将SQL字符串解析为多行VBA代码,并使用续行?我使用字符串(忽略|)|从表1中选择Table1.*,Table1.FldA,其中((Table1.FldA)=“A1”)|它从表1转换为| strSql=“SELECT Table1.*,Table1.FldA,其中((Table1.FldA)=“A1”);“|是-我试图获取一个有效的SQL字符串,并将其转换为一个可以通过VBA执行的字符串。很抱歉出现混淆,以确保只有一行返回“SELECT Top 1…”。要测试1个以上,请在打开RS后:| RS.MoveLast | RS.MoveFirst | Debug.Print RS.Recordcount|
SELECT 
TT.TEST_TABLE_ID,
TT.TEST_TABLE_NO,
TT.MEMBERSHIP_NUMBER,
TT.TEST_TABLE_TYPE,
from TEST_TABLE TT
Sub GetData()
Dim Conn As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim sqlText As String
Dim Row As Long
Dim Findex As Long
Dim Data As Worksheet
Dim X As Long
    Set Data = Sheets("Results")
    Data.Select
    Cells.ClearContents
    Conn.Open "PROVIDER=ORAOLEDB.ORACLE;DATA SOURCE=ORCL;USER ID=user;PASSWORD=password"
    cmd.ActiveConnection = Conn
    cmd.CommandType = adCmdText
    'sqlText = How to reference Valid SQL cells
    cmd.CommandText = sqlText
    Set RS = cmd.Execute
    For X = 1 To RS.Fields.Count
        Data.Cells(1, X) = RS.Fields(X - 1).Name
    Next

    If RS.RecordCount < Rows.Count Then
        Data.Range("A2").CopyFromRecordset RS
    Else
        Do While Not RS.EOF
           Row = Row + 1
           For Findex = 0 To RS.Fields.Count - 1
             If Row >= Rows.Count - 50 Then
                Exit For
             End If
             Data.Cells(Row + 1, Findex + 1) = RS.Fields(Findex).Value
           Next Findex
           RS.MoveNext
        Loop
    End If
    Cells.EntireColumn.AutoFit
End Sub
Function SQLQueryRun(ByVal query As String, ByVal returnData As Boolean) As Variant
Dim Conn As New ADODB.Connection
Dim ADODBCmd As New ADODB.Command
Dim ret As New ADODB.Recordset

    Conn.ConnectionString = "connection_string_here"
    Conn.Open
    ADODBCmd.ActiveConnection = Conn
    ADODBCmd.CommandText = query
    Set ret = ADODBCmd.Execute()
    If returnData Then
        If Not ret.EOF Then SQLQueryRun = ret.GetRows()
    Else
        SQLQueryRun = True
    End If
    Conn.Close
    Set Conn = Nothing
    Set ret = Nothing
End Function
Dim cnn As New ADODB.Connection
Dim connectionString As String
Dim cmd As New ADODB.Command
Dim c As Range, ws As Worksheet
Dim rst as ADODB.Recordset

connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=C:\Database3.accdb;Persist Security Info=False;"
cnn.Open connectionString
cmd.ActiveConnection = cnn

For Each c In ws.Range()
    cmd.CommandText = c.Value
    set rst = cmd.Execute 
    'do what you need to with your new recordset before moving on to the next SELECT
Next c
Option Explicit

Sub Process_SQL_Strings()
Dim cmd         As New ADODB.Command
Dim sqlText     As String
Dim Row         As Long
Dim Findex      As Long
Dim Data        As Worksheet
Dim iFldCt      As Long
Dim conn        As ADODB.Connection
Dim rs          As ADODB.Recordset
Dim sConn       As String
Dim lLastRow    As Long
Dim lRow        As Long

    Set Data = Sheets("Results")
    Data.Select
    Cells.ClearContents
    conn.Open "PROVIDER=ORAOLEDB.ORACLE;DATA SOURCE=ORCL;USER ID=user;PASSWORD=password"
    cmd.ActiveConnection = conn
    cmd.CommandType = adCmdText

''        Set conn = New ADODB.Connection
''        sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
''                                 "Data Source=C:\data\access\tek_tips.accdb;" & _
''                                 "Jet OLEDB:Engine Type=5;" & _
''                                 "Persist Security Info=False;"
    conn.Open sConn

    'sqlText = How to reference Valid SQL cells
    lRow = 1
    Do
        sqlText = Sheets("SQL").Range("A" & lRow)
        If sqlText = "" Then
            MsgBox "Finished processing " & lRow & " rows of SQL", vbOKOnly, "Finished"
            GoTo Wrap_Up
        End If

        Set rs = New ADODB.Recordset
        rs.Open sqlText, conn, adOpenStatic, adLockBatchOptimistic, adCmdText

        Data.Cells(lRow, 1) = sqlText

       If not rs.EOF then
        For iFldCt = 1 To rs.Fields.Count
            Data.Cells(lRow, 1 + iFldCt) = rs.Fields(iFldCt - 1).Name
        Next

        If rs.RecordCount < Rows.Count Then
            Data.Range("B" & lRow).CopyFromRecordset rs
        Else
            Do While Not rs.EOF
               Row = Row + 1
               For Findex = 0 To rs.Fields.Count - 1
                 If Row >= Rows.Count - 50 Then
                    Exit For
                 End If
                 Data.Cells(Row + 1, Findex + 1) = rs.Fields(Findex).value
               Next Findex
               rs.MoveNext
            Loop
        End If
        Cells.EntireColumn.AutoFit
      End If
        lRow = lRow + 1
    Loop
Wrap_Up:
    rs.Close
    Set rs = Nothing
    conn.Close
    Set conn = Nothing
End Sub