1

I'm trying to import an SQL file into excel via VBA in order to return the SQL code as one string, however I'm getting tiny squares (carriage returns?) between each character when I import the SQL as a string - not a clue why! It's worked before in other files, with different SQL, so I'm not sure what I've done wrong.

Function ImportSQLText(FileExt, FileSQLName)
'//Needs Microsoft Scripting Runtime Ref
'//and microsoft active x data objects 2.8

Dim ReadText As String, newLine As String
Dim fso As FileSystemObject: Set fso = New FileSystemObject

Set txtStream = fso.OpenTextFile(FileExt & fileName, ForReading, False)

ReadText = " "
Do While Not txtStream.AtEndOfStream
    newLine = txtStream.ReadLine
    If InStr(newLine, "[input-sDte]") > 0 Then

        ReadText = ReadText & Replace(newLine, "[input-sDte]", sDte) & vbCrLf
    ElseIf InStr(newLine, "[input-eDte]") > 0 Then
        ReadText = ReadText & Replace(newLine, "[input-eDte]", eDte) & vbCrLf
    Else
         ReadText = ReadText & newLine & vbCrLf
    End If

Loop
txtStream.Close

ImportSQLText = ReadText

End Function

The SQL code looks like:

    ;if object_id('tempdb.#usr_fld') is not null

    drop table #usr_fld

    -- Creating Temp Table
    CREATE TABLE #usr_fld([PLACE-REF] NVARCHAR(50)
    ,[PROJCODE] nvarchar(100)
    ,[CUORACLE] nvarchar(100)); 

Any input would be greatly appreciated - googling it is not helping me this time.

1
  • 1
    "getting tiny squares between each character" -> probably reading UTF16 as ANSI/ASCII/other 8 bit encoding - try use a hex editor to check source file. Commented May 8, 2018 at 13:49

1 Answer 1

2

You almost definitely have a unicode file. There is an optional fourth parameter on the OpenTextfile method of FSO:

 Const TriStateTrue = -1
 Set txtStream = fso.OpenTextFile(FileExt & fileName, ForReading, False, TriStateTrue)

Just swap out your line for these two and it might fix the issue.

Sign up to request clarification or add additional context in comments.

5 Comments

Nice. I'd try that before rewriting everything to use an ADODB stream, although the documentation seems to rather explicitly imply that FSO doesn't handle unicode.
Agreed. Jet/Ace are so good at making bad decisions on text and excel files too. Before you know it you are writing a schema.ini on the fly and dropping System DSNs into registry on run time just to make things work. Although, I do love me some ADODB.
hmm, this answer and its comments strongly imply that FSO wouldn't handle UTF-8 encoding (UTF-8 != Unicode), making ADODB.Stream the only remaining option, depending on the actual encoding of OP's file. FWIW ADODB.Stream has nothing to do with Jet/Ace or databases, it's just a file stream ;-)
I'm holding out that the TriStateTrue parameter will do the trick. UTF-16LE which is the windows default unicode is a two-byte scheme and OP is saying there is one extra character popping up in between each character. UTF-8 is three bytes (but is often only two) so... it's a 50/50. Didn't know that about ADODB.stream. I'll have to read up on that one.
Fantastic! That got it - thank you so much! Had me completely flummoxed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.