+ Reply to Thread
Results 1 to 4 of 4

remove space in empty variable when using printToFile

Hybrid View

cjg123 remove space in empty... 08-12-2011, 08:05 AM
JBeaucaire Re: remove space in empty... 08-12-2011, 04:16 PM
DonkeyOte Re: remove space in empty... 08-12-2011, 04:23 PM
cjg123 Re: remove space in empty... 08-15-2011, 11:45 AM
  1. #1
    Registered User
    Join Date
    08-12-2011
    Location
    minneapolis
    MS-Off Ver
    Excel 2003
    Posts
    2

    Question remove space in empty variable when using printToFile

    Hi,
    I have data in a worksheet with 3 columns:
    group_name, group_order, and description

    In my macro have the following code:
    With TCode(i)
    printToFile "INSERT INTO t_codes VALUES"
    printToFile " ('" & .group_name & "', " & .group_order & ", '" & .description & "');" End With

    which outputs to a file as:
    INSERT INTO t_codes VALUES
    (1, 0, 'red');

    However, if one of the columns does not have data, e.g. description is blank,
    the output is:
    INSERT INTO t_codes VALUES
    (1, 0, ' ');

    where the description field now has a space as the value. When this file then is run into the database, the end result is incorrect...it *appears* there is data, i.e. a space, rather than a null value (hope that makes sense)

    Is there any way I can remove this space value within in the printToFile?
    Thanks,
    cjg
    Last edited by cjg123; 08-15-2011 at 11:44 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: remove space in empty variable when using printToFile

    Don't write the value directly into the file. Construct the string first, then use a REPLACE function on the string to replace ' ' with '', then write the string into the file.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

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

    Re: remove space in empty variable when using printToFile

    As is your code should not be generating a space unless .description is itself a space (apply TRIM) ... the code should otherwise generate '' rather than ' '

    Also when you say Null do you mean Null String or DB Null ?

  4. #4
    Registered User
    Join Date
    08-12-2011
    Location
    minneapolis
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: remove space in empty variable when using printToFile

    Thanks, I'll give it a try!!

+ 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