+ Reply to Thread
Results 1 to 11 of 11

Run-Time error '1004' .refresh background query:= False

  1. #1
    Registered User
    Join Date
    04-14-2013
    Location
    henderson, tennessee
    MS-Off Ver
    365
    Posts
    42

    Run-Time error '1004' .refresh background query:= False

    Hi all,

    I am pretty new to vba and am having trouble. I have chopped, slopped and butchered a macro from another program trying to suit my needs and am having difficulty in making it work like I want it to. With a fresh sheet it works the first time. When I try it a second time I get the run-time error and when I try to debug it, vba points me to the .refresh backgroundquery:= False line. My macro pulls data from one tab in my workbook and places it in column A, everything else is pulling from a SQL database. I will try to provide a screenshot of the worksheet and the code. I want all of the data from A2:J17 to move down and be replaced with new data when I update and I want row 1 to remain stationary. Any ideas where I am going wrong?

    Sub Update_Names()


    Sheets("Data Entry").Select
    ActiveWindow.SmallScroll Down:=54
    Range("F75:F82").Select
    Selection.Copy

    Sheets("18-02 Sliving").Select
    Range("A2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    'Forming 18-02***********************************
    Sheets("Data Entry").Select
    Range("F85:F92").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("18-02 Sliving").Select
    Range("A10").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    'Columns("A:A").EntireColumn.AutoFit
    'Range("B14").Select


    Dim strSQL As String
    Dim V_Beg_Date As String
    Dim V_End_Date As String

    V_Beg_Date = Sheets("Data Entry").Range("D3")
    V_End_Date = Sheets("Data Entry").Range("D4")



    Sheets("18-02 Sliving").Select
    'Cells.Select
    'Selection.Delete Shift:=xlUp
    'Selection.Delete Shift:=xlUp
    'Selection.Clear
    'Selection.ClearFormats
    'ActiveWindow.FreezePanes = False
    Range("B1").Select


    strSQL = "SELECT TIMESTAMP, CHOPPER, SHIFT_CODE 'Shift', OE , "
    strSQL = strSQL & "CE, BBOH, HTB, QTY_ON_HOLD '# on Hold', "
    'strSQL = strSQL & "NUM_CHUTE_JAMS '# CJs', AVG_CHUTE_JAM_TIME 'Chute Downtime', "
    'strSQL = strSQL & "TOT_CHUTE_JAM_TIME 'Total CJ Time', TOT_NET_BTA 'Net BTA', "
    'strSQL = strSQL & "TOT_NET_FTA 'Net FTA', TOT_ROPE_OCC '# Rope Occ', "
    'strSQL = strSQL & "TOTAL_LC_OCC '# Long Chop Occ', COT_CHANGES '# Cot Changes', "
    strSQL = strSQL & "CHOP_CHECK_PCT 'Chop Check %'"
    strSQL = strSQL & "From dbo.SHIFT_SUMM_CHPRDATA2 "
    strSQL = strSQL & "Where ""timestamp"" >= '" & V_Beg_Date & "' "
    strSQL = strSQL & "and ""timestamp"" < '" & V_End_Date & "'"



    server = "ODBC;DSN=***_***"



    'On Error Resume Next
    'ActiveSheet.QueryTables(1).Delete
    'On Error GoTo 0

    With ActiveSheet.QueryTables.Add(server, _
    Destination:=Range("B1"))
    .Sql = (strSQL)
    .FieldNames = True
    .RefreshStyle = xlInsertDeleteCells
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .RefreshOnFileOpen = False
    .HasAutoFormat = True
    .BackgroundQuery = True
    .TablesOnlyFromHTML = True
    .Refresh BackgroundQuery:=False
    .SavePassword = True
    .SaveData = True
    End With


    End Sub
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    04-14-2013
    Location
    henderson, tennessee
    MS-Off Ver
    365
    Posts
    42

    Re: Run-Time error '1004' .refresh background query:= False

    Upon reflection I feel as though my thread post wasn't catchy enough. Lot of lookers but no takers. Is there anything I can clarify to help jog an answer for anyone? I have been playing and learning with this thing for about a week now. Would really appreciate any sort of direction at all. :-)

  3. #3
    Registered User
    Join Date
    04-14-2013
    Location
    henderson, tennessee
    MS-Off Ver
    365
    Posts
    42

    Re: Run-Time error '1004' .refresh background query:= False

    Ok I think I may have learned something. I believe my problem has something to do with Query Tables. Something to do with a cell or cells being associated with a range or multiple query tables? Anyone have any ideas?

  4. #4
    Registered User
    Join Date
    04-14-2013
    Location
    henderson, tennessee
    MS-Off Ver
    365
    Posts
    42

    Re: Run-Time error '1004' .refresh background query:= False

    Would I be better off utilizing an Access database for this? I envision that after a few months, the scope of data I would have archived might exceed the limitations of an excel spreadsheet. Any thoughts anyone?

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Run-Time error '1004' .refresh background query:= False

    is there a reason why you commented out the code to delete any existing querytable before adding a new one in the same location?
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  6. #6
    Registered User
    Join Date
    04-14-2013
    Location
    henderson, tennessee
    MS-Off Ver
    365
    Posts
    42

    Re: Run-Time error '1004' .refresh background query:= False

    That was never in the original macro and I just tried it to see if it would work. The original macro is in another program that doesn't save the data at all, it's merely a tool for running a query and is locked out as a read only tool. I scavenged the macro to try and make my own because I didn't know how to script the code for accessing the sql db. And it didn't work by the way.(The code to delete any existing querytable before adding a new one in the same location)

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Run-Time error '1004' .refresh background query:= False

    are you using the same criteria values each time you run it? that error would usually imply a problem with the query

  8. #8
    Registered User
    Join Date
    04-14-2013
    Location
    henderson, tennessee
    MS-Off Ver
    365
    Posts
    42

    Re: Run-Time error '1004' .refresh background query:= False

    No sir. Criteria is pretty simple. I create a "shift schedule" which consists of my putting people's names into cells in a spreadsheet with a time and date range. All names change everyday as does the time and date range. Then the macro uses the date range to pull data from a sql db given the time and date range as a "timestamp." It will work if I delete everything from the spreadsheet that the data is populated to and it is completely blank. But when I try to run it a second time, it gives me the run time error and says it cannot insert the columns because column IV contains data and you will not get any data. I am doing something wrong, just don't know enough to figure it out yet.

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Run-Time error '1004' .refresh background query:= False

    do you have a particular reason for using a querytable when it seems you do not require a live link to the database?

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Run-Time error '1004' .refresh background query:= False

    to answer your other question-yes I reckon you'd be better off with any sort of real database program (although I don't know why you're extracting data from a database to archive it in another database)

  11. #11
    Registered User
    Join Date
    04-14-2013
    Location
    henderson, tennessee
    MS-Off Ver
    365
    Posts
    42

    Re: Run-Time error '1004' .refresh background query:= False

    The current sql db table that archives the performance numbers I want to track doesn't associate a person's name with the numbers. I'm sure you can somehow edit the sql table but we don't have a database administrator here where I work nor do we have anyone that can easily help us out. I am merely trying to learn this stuff out of necessity. I just want to be able to track a person's performance easily. When the database was first set up there was no interface created for that purpose and now there is a need for it.

+ 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