+ Reply to Thread
Results 1 to 3 of 3

Need help copying a variable into a query

  1. #1
    Registered User
    Join Date
    05-07-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    3

    Need help copying a variable into a query

    I think what I need is really simple but I'm new to VBA and am lost at this point .

    I want to be able to input a Name into a cell (or input box??), click "GO", and it will query Quickbooks to pull all the transactions for only that name. I've tried Range.Copy but it doesn't copy to the MS query. Here's the pertinent coding - "B4" is where I currently have the name, and "Linda Jones" is where that variable needs to go (both in red below). Thanks for any and all answers!


    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
    Range("B4").Copy

    With ActiveWorkbook.Connections("Contacts Fake Company").ODBCConnection
    .BackgroundQuery = True
    .CommandText = Array( _
    "SELECT v_lst_names.name_type, v_lst_names.name, v_lst_contacts.telephone_num1, v_lst_contacts.email, v_txn_sales_re" _
    , _
    "ceipt_hdr.transaction_date_h, v_txn_sales_receipt_line.unit_price_amt, v_txn_sales_receipt_line.amount_amt, v_txn_s" _
    , _
    "ales_receipt_line.memo, v_txn_sales_receipt_line.quantity_qnty" & Chr(13) & "" & Chr(10) & "FROM QBReportAdminGroup.v_lst_contacts v_lst_contac" _
    , _
    "ts, QBReportAdminGroup.v_lst_names v_lst_names, QBReportAdminGroup.v_txn_sales_receipt_hdr v_txn_sales_receipt_hdr," _
    , _
    " QBReportAdminGroup.v_txn_sales_receipt_line v_txn_sales_receipt_line" & Chr(13) & "" & Chr(10) & "WHERE v_lst_names.id = v_lst_contacts.name_i" _
    , _
    "d AND v_txn_sales_receipt_hdr.customer_id_h = v_lst_names.id AND v_txn_sales_receipt_line.doc_num_h = v_txn_sales_r" _
    , _
    "eceipt_hdr.doc_num_h AND ((v_lst_names.name_type='Customer') AND (v_lst_names.name='Linda Jones'))" & Chr(13) & "" & Chr(10) & "ORDER BY v_lst" _
    , "_names.name, v_txn_sales_receipt_hdr.transaction_date_h DESC")

  2. #2
    Valued Forum Contributor
    Join Date
    11-20-2012
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2010
    Posts
    597

    Re: Need help copying a variable into a query

    instead of Range("B4").copy you should have a variable ie nm = range("B4").value
    and where 'Linda Jones' is you should have " & nm & "

    if that doesn't work try nm without the " & & " around it

  3. #3
    Registered User
    Join Date
    05-07-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Need help copying a variable into a query

    THANKS!! It worked with the " & & " around them. *whew* I'm now a happy camper

+ 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