0
votes

I have a question about VBA mainly in this. I recorded a macro, when creating a query connection to an SQL server.

In my Sheet (Sheet1) I have listed a couple of values, which tells the name of the SQL Server, the Database, the Table name and the year value (being 2019).

It works almost great, but I get some issues with the Source coding. See below:

    Sub Macro1()
Dim sht As Worksheet
Set sht = Sheets("Sheet1")
Dim SQLSRV, DBase, YValue, Table As String

SQLSRV = sht.Range("B:B").Find("SQL Server").Offset(0, 1).Value
DBase = sht.Range("B:B").Find("Database").Offset(0, 1).Value
YValue = sht.Range("B:B").Find("Year").Offset(0, 1).Value
Table = sht.Range("B:B").Find("Acccount").Offset(0, 1).Value


    ActiveWorkbook.Queries.Add Name:="VAT Sales", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Sql.Database(" & SQLSRV & ", " & DBase & ", [Query=""select [Posting Date]#(lf)" & _
        "" & "        ,[G_L Account No_]#(lf)        ,[Document No_]#(lf)        ,[Description]#(lf)        ,[Amount]#(lf)        ,[VAT Amount]#(lf)" & _
        "" & "        ,[Source Code]#(lf)        ,[VAT Bus_ Posting Group]#(lf)        ,[VAT Prod_ Posting Group]#(lf)        ,[Gen_ Posting Type]#(lf)" & _
        "" & "        ,[External Document No_]#(lf)        ,[Source No_]#(lf)        ,[User ID]#(lf)        ,[Unique Document No_]#(lf)        ,[Open Kvik]#(lf)" & _
        "" & "        ,[Remaining Amount Kvik]#(lf) from [" & DBase & "].[dbo].[" & Table & "$G_L Entry]#(lf)where [Posting Date] >= '01-01-" & YValue & "'""])" & _
        "" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Source"
    End Sub

My issue comes at the section " Source = Sql.Database(" & SQLSRV & ", " & DBase & ", …
For Power Query to read this correctly I need to be able to insert a quote at the beginning, and at the end of the variable, and still be able to transfer the value of the variable.

Is there some kind of Chr() that I'm not aware of?

Thank you in advance :)

1

1 Answers

0
votes

I found out afterwards that Chr(34) does the trick :)