Results 1 to 4 of 4

Thread: Using String value in an sql statement.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    295

    Using String value in an sql statement.

    Hi All,

    I have below sql statement in my excel macro I want to define string for mydatabase name instead of hardcoding it's name in the below sql statement.

    My database name is : mydata
    I am trying get the above name from a combobox selection.

    Dim mydata As String
    mydata = Userform1.Combobox4.Value

    Can you guys tell me how to use the above string in the below sql statement so that it accepts the value selected by user in a combobox as a database table name.

    Code:
    cmd1.CommandText = "SELECT mydata.*, CRM.Country, CCM.[Sub Product UBR Code], CEM.FSI_LINE3_code FROM Data_SAP.dbo.mydata mydata INNER JOIN Data_SAP.dbo.[Country_Region Mapping] CRM  ON (mydata.[Company Code] = CRM.[Company Code])INNER JOIN Data_SAP.dbo.[Cost Center mapping] CCM  ON (mydata.[Cost Center] = CCM.[Cost Center])INNER JOIN Data_SAP.dbo.[Cost Element Mapping] CEM  ON (mydata.[Unique Indentifier 1] = CEM.CE_SR_NO)WHERE CRM.Country IN (" & selection1 & ") AND CCM.[Sub Product UBR Code] IN (" & selection & ") AND CEM.FSI_LINE3_code IN (" & selection2 & ")AND mydata.year = '" & ComboBox4.Value & "' AND mydata.period = '" & ComboBox3.Value & "'AND mydata.[Document Type]= '" & Left(ComboBox11.Value, 2) & "'
    Thanks a lot for your help in advance.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Using String value in an sql statement.

    The same way that you are using other variables - end the string (with " ), append the variable to it (with & ), then re-start the string (with " ).

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    295

    Re: Using String value in an sql statement.

    Hi Geek,

    Thanks a lot for your reply, Is the below way the correct way to write it ?.

    Existing line of code :
    Code:
    cmd1.CommandText = "SELECT mydata.*
    Modified Line of Code :
    Code:
    cmd1.CommandText = "SELECT " & mydata & ".*
    Thanks a lot for your help in advance.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Using String value in an sql statement.

    That is correct.

Posting Permissions

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



Click Here to Expand Forum to Full Width