+ Reply to Thread
Results 1 to 6 of 6

SQL returns blank instead of text

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-04-2004
    MS-Off Ver
    2013
    Posts
    148

    SQL returns blank instead of text

    Hi!

    I'm using SQL to retrieve a bunch of data from another Excel file. However, SQL only returns numbers, leaving text records with blanks ("").

    This only occurs in one column, which has numbers and alphanumeric values.

    Any ideas on what is happening and how can I fix this?

    Thanks in advance!
    Last edited by pmguerra; 03-30-2009 at 12:50 PM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: SQL returns blank instead of text

    It might help to post your code (remember to use CODE tags)

    I've found when using ADO that it's sometimes better to iterate through the recordset when handling datasets contain text fields than it is to return the entire recordset in one go ... in my experience mediumtext/longtext fields tend to get dropped... ie it's slower to process (not horrendously) but you get everything returned.... my experience is talking to MySQL/SQLSvr etc but the premise may be the same dealing with other files (I honestly don't know).

    There may be other solutions... the above is just my 2 cents.

  3. #3
    Forum Contributor
    Join Date
    10-04-2004
    MS-Off Ver
    2013
    Posts
    148

    Re: SQL returns blank instead of text

    Thank you for your reply.

    My code is very simple, I used the Macro Recorder to do the code for me and then customized the SQL statement to my needs.

    origemSQL = "My Path Here"
    comSQL = "SELECT DISTINCT TDadosRecolha.Circuito, TDadosRecolha.Mês,  TDadosRecolha.`Tipo Deposição`, TDadosRecolha.Fileira, TDadosRecolha.`Frequência Recolha`" & Chr(13) & _
                "FROM `" & origemSQL & "`.TDadosRecolha TDadosRecolha" 
    
    origemSQL = "ODBC;DSN=Ficheiros Excel;DBQ=" & origemSQL & ";DriverId=790;MaxBufferSize=2048;PageTimeout=5;"
    
    With ActiveSheet.QueryTables.Add(Connection:=origemSQL, _
            Destination:=Range("A1"))
            .CommandText = comSQL
            .Name = "Consulta 2 de Ficheiros Excel"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
                .Refresh BackgroundQuery:=False
        End With

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: SQL returns blank instead of text

    See: http://support.microsoft.com/kb/257819 ... in particular I guess I'm referring to the Considerations section... this is outside my expertise so I can't help further -- hopefully others can assist as necessary...

  5. #5
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: SQL returns blank instead of text

    One of the major differences between databases and spreadsheets is that
    databases have tables with narrowly defined data types: Text, Integers, etc.
    If a field is defined as text...only text is allowed in that field.

    Your ADO connection will attempt to figure out the kind of data
    your columns contain and will ignore other types. If the first few rows of
    your data list contain mostly numbers, it will assume numeric data is in the column
    and ignore text. If the first few rows contain mostly text, numeric data will be ignored.

    The "fix" is to convert all of the column data to one type. Since you have a
    mix of numbers and text...convert the numbers to text.
    Here's one way:
    Select the column of values.
    Data.Text-to-columns...Click: Next....Click: Next
    Select the data column...Check: Text...Click: Finish

    Does that help?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  6. #6
    Forum Contributor
    Join Date
    10-04-2004
    MS-Off Ver
    2013
    Posts
    148

    Re: SQL returns blank instead of text

    Ok, that works fine! I converted all the values to TEXT and now all is well

    Thank you for your help, Ron and DonkeyOte!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1