473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Run time error-1004 on query tables.add connections - need help.

Expand|Select|Wrap|Line Numbers
  1. Sub macro1()
  2. 'Dim frm As UserForm
  3. Dim I As Integer
  4. 'To could be as many as you want. I only put 25 as a limit.
  5. For I = 1 To 25
  6. Dim a As String
  7. a = Sheet1.Cells(I, 1).Value
  8. Dim b As String
  9. b = ActiveSheet.Cells(I, 2).Value
  10. 'get URLs
  11. 'option after "URL;" & a
  12.     'UserForm1.txtbox1.value
  13. With ActiveWorkbook.ActiveSheet.QueryTables.Add(Connection:="URL;" & a, Destination:=Range("$A$1"))
  14.  'With ActiveWorkbook.ActiveSheet.QueryTables.Add(Connection:="URL;" & a, Destination:=Range(b))
  15.         .Name = _
  16.         "its_details_value_node.html?nsc=true&listId=www_s201_b9233&tsId=BBK01.ED0439"
  17.         .FieldNames = True
  18.         .RowNumbers = False
  19.         .FillAdjacentFormulas = False
  20.         .PreserveFormatting = True
  21.         .RefreshOnFileOpen = False
  22.         .BackgroundQuery = True
  23.         .RefreshStyle = xlInsertDeleteCells
  24.         .SavePassword = False
  25.         .SaveData = True
  26.         .AdjustColumnWidth = True
  27.         .RefreshPeriod = 0
  28.         .WebSelectionType = xlEntirePage
  29.         .WebFormatting = xlWebFormattingNone
  30.         .WebPreFormattedTextToColumns = True
  31.         .WebConsecutiveDelimitersAsOne = True
  32.         .WebSingleBlockTextImport = False
  33.         .WebDisableDateRecognition = False
  34.         .WebDisableRedirections = False
  35.         .Refresh BackgroundQuery:=False
  36.         'MsgBox "Done!"
  37.     End With
  38.  Next I
  39.  
  40. 'close all forms
  41.     'For Each frm In UserForms
  42.  
  43.      '   Unload frm
  44.  
  45.     'Next frm
  46.     End Sub
  47.  

Hello,

Code above is having issue when ran from a form. The code works if it's ran from spreadsheet however when placed in a button in the form it fails with error code 1004. Please help.

I've researched and most of the blogs weren't very helpful.

Thanks.

I use this VBA to pull URLs from Sheet one (craigslist URL) then scrape it up to 25 sites using loop function. This works when manually placed in sheet but not in button of excel.
Nov 13 '15 #1
16 5079
zmbd
5,501 Expert Mod 4TB
what this is saying is that something isn't formatted correctly.

Just to be sure, this is the exact same code that works in the worksheet level module? What I would do here is instead of referencing "activesheet" I would reference the worksheet either by name or index. It's possible that the sheet is not being seen as active when the form has loaded.
Nov 13 '15 #2
Not working. I would get the run-time error '-2147024809 (80070057)';
"The destination range is not on the same worksheet that query table is being created on."

What I'm trying to accomplish is as it scrapes craigslists URL 1st page, it pastes it in a worksheet then moves to creating a new sheet for the new scraped data.

Thank you.

-Al
Nov 15 '15 #3
hvsummer
215 128KB
don't use activesheet on another application that call for excel.
replace "ActiveSheet.Cells(I, 2).Value"
with fixed name that link to your range.
similar to another code that use activesheet.

if you run code in background of another application, don't use .ActiveSheet or .ActiveWorkbook ==
Nov 15 '15 #4
zmbd
5,501 Expert Mod 4TB
aflores41,
Although the error "The destination range ... worksheet.." leads me to believe that my assumption that you are instancing the form from with Excel; never-the-less, will you please clarify this for us: are you instancing the form from within Excel, Access, or some other program?

What this error tells me is that the workbook or worksheet is losing the focus within your code or that the connection string in 13 is not resolving correctly.

You have to explicitly set the reference to these objects when called from the form.


hvsummer:
Take a look at the error, this appears to be an Excel generated failure, not one I would expect if called from within another application.
Nov 15 '15 #5
hvsummer
215 128KB
oh, sorry Aflores and zmbd, I have read this "This works when manually placed in sheet but not in button of excel"

So the first thing I have to say, you posted this question in wrong section...(should be excel section)
Secondly, where is that button on ? your original sheet that you did place code in or somewhere else ?

since this vba from excel, it will work different from Access vba...
Nov 15 '15 #6
zmbd
5,501 Expert Mod 4TB
No worries, this is VBA related; thus, it is, technically, in the correct forum. =)
If it had not have been, then I would have moved it :)

There are some Excel specific objects and references; however, still VBA.

Anyway, I'll have to take another look thru the original code. A direct reference should have been the ticket.

aflores41:
I need you to open your form
Show the properties dialog for the control button you are attaching this to.
Is the property, "TakeFocusOnClick" set to "true"

The next thing I need is for you to post the exact code that the button us using not the Sub() you've posted we need to see the
Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandButton1_Click()
  2. ''codehere
  3. End Sub
If you have instead designed a "form" using a worksheet and inserting objects therein, then we have a whole other beast to work with and it should explain what is happening a bit more clearly.
Nov 15 '15 #7
NeoPa
32,556 Expert Mod 16PB
Al, we've all assumed that the error is being triggered from line #13. It would be helpful if you'd confirm this, and also include any error message with your questions (rather than just a number), for those of us that don't carry the error table around in our heads ;-)
Nov 16 '15 #8
Expand|Select|Wrap|Line Numbers
  1. Sub Scrape_sheet1_btn_Click()
  2. Dim LResponse, I As Integer
  3. Dim a, b As String
  4.  
  5. LResponse = MsgBox("You must list the URLs you are planning to scrape in sheet1 and create a blank sheet2. Once the scrape macro starts, each column is represented by individual URLs from sheet1." & Chr(13) & Chr(13) & "Do you wish to continue?", vbYesNo, "Continue")
  6. If LResponse = vbYes Then
  7.  
  8. 'To 25 could be as many as you want. I only put 25 as a limit.
  9.     For I = 1 To 25
  10.  
  11. 'make sure you have URLs in sheet1 from cell A1 listed.
  12.     a = Sheet1.Cells(I, 1).value
  13. 'activesheet paste
  14.     b = ActiveSheet.Cells(I, 2).value
  15.  
  16.  
  17. 'get URLs
  18.  
  19.     With ActiveSheet.QueryTables.Add(Connection:="URL;" & a, Destination:=Range("$A$1"))
  20.         '.Name = _
  21.         '"its_details_value_node.html?nsc=true&listId=www_s201_b9233&tsId=BBK01.ED0439"
  22.         .FieldNames = True
  23.         .RowNumbers = False
  24.         .FillAdjacentFormulas = False
  25.         .PreserveFormatting = True
  26.         .RefreshOnFileOpen = False
  27.         .BackgroundQuery = True
  28.         .RefreshStyle = xlInsertDeleteCells
  29.         .SavePassword = False
  30.         .SaveData = True
  31.         .AdjustColumnWidth = True
  32.         .RefreshPeriod = 0
  33.         .WebSelectionType = xlEntirePage
  34.         .WebFormatting = xlWebFormattingNone
  35.         .WebPreFormattedTextToColumns = True
  36.         .WebConsecutiveDelimitersAsOne = True
  37.         .WebSingleBlockTextImport = False
  38.         .WebDisableDateRecognition = False
  39.         .WebDisableRedirections = False
  40.         .Refresh BackgroundQuery:=False
  41.     End With
  42. Next I
  43. MsgBox "Done!"
  44. Else: End
  45. End If
  46. End Sub
Hello,

Thank you for being patient with me. I apologize for a late response. Above is the vba code I used for the button I'm using as an add-on in excel. The vba code is encoded in the xlam file.

NeoPa, you're right for the line#13 as the error however, I don't know how to fix it. I've tried changing activesheets to sheet1 or sheet1(sheetname) and still the same it won't work.

Thank you.

Al
Nov 18 '15 #9
NeoPa
32,556 Expert Mod 16PB
AFlores:
you're right for the line#13 as the error
OK Al. And the error message is ... ?
Nov 18 '15 #10
Run time error-1004 (error in line 40) as activesheets.
run-time error '-2147024809 (80070057)' (error in line 19) as sheet1....
run time error 438 (error in line 19) as sheet1(sheet1)
Nov 18 '15 #11
zmbd
5,501 Expert Mod 4TB
Referring to the code-block in Post#9

It shouldn't make a difference; however, I would break Line 3 apart or explicitly cast "a" as string. Starting with Acc2007, the comma designation no longer returns the same cast type for a group of variables. Variable "a" is being cast as type variant whereas "b" is actually being cast as type string.
(see example here : MS>Declaring Variables
You can declare several variables in one statement. To specify a data type, you must include the data type for each variable. In the following statement, the variables intX , intY , and , intY , and intZ are declared as type Integer.
Expand|Select|Wrap|Line Numbers
  1. Dim intX As Integer, intY As Integer, intZ As Integer 
In the following statement, intX and intY are declared as type Variant; only and intY are declared as type Variant; only intZ is declared as type Integer.
Expand|Select|Wrap|Line Numbers
  1. Dim intX, intY, intZ As Integer 
You don't have to supply the variable's data type in the declaration statement. If you omit the data type, the variable will be of type Variant.
Although one may still be able to "bulk" type variables in VB such does not hold true for VBA.

Line15 insert debug.print "Range-A: " & a
Line16 insert debug.print "Range-B: " & b
We need to see what is actually being returned by these calls as I suspect your connection string is malformed.
Line20/21 may also be giving you some issues with the non-alphanumeric values. It is usually advisable not to use anything other than alphanumeric and the underscore in connection names - also highly advisable not to use spaces in the name either.

errors reported for line40 are most likely related to line19.

Finally I always advise building the string first and then inserting that as needed, Thus line19 transforms

Expand|Select|Wrap|Line Numbers
  1. '...air code...
  2. '...other code...
  3. Dim zstrCN as String
  4. '
  5. '...other code...
  6. '
  7. zstrCN = "URL;" & a
  8. '
  9.  With ActiveSheet.QueryTables.Add(Connection:= zstrCN, _
  10.    Destination:=Range("$A$1"))
This way you can place a debug.print zstrCN on the line following the definition to ensure that the connection string is resolving correctly. Even a single space, or even letter-case, can cause these to fail.
Nov 18 '15 #12
NeoPa
32,556 Expert Mod 16PB
Hi again Al. I think there's a little confusion here still.

I don't want to flog a dead horse, but what I was talking about would have been some thing like "Object doesn't support this property or method" for error #438. I don't have the other messages to hand, but I hope that gives you the picture for what can be very helpful in getting meaningful answers to your questions.
Nov 18 '15 #13
NeoPa
32,556 Expert Mod 16PB
ZMBD:
Starting with Acc2007, the comma designation no longer returns the same cast type for a group of variables. Variable "a" is being cast as type variant whereas "b" is actually being cast as type string.
FYI:
As far as I'm aware this is how it's always been and hasn't changed. Certainly that's true for 2003 that I can test, but I seem to recall it being the case from 97 which was when I started with Access again after a long gap. I would guess it's the same all the way from the start TBF.
Nov 18 '15 #14
zmbd
5,501 Expert Mod 4TB
I remember that in either ACC2003 or 2010, we jumped from Office-95, I had a few modules break because I had "bulk" defined the variables... the more I think about it the more I sure it was the ACC2003 jump... in any case I was expecting "long" and the variables were coming thru as "strings" and of-course when storing the values to the table Error 13 - Type Mismatch. Just about pulled my hair out trying to find that error! :)
Nov 19 '15 #15
NeoPa
32,556 Expert Mod 16PB
I can't imagine why they broke at that stage as an untyped variable is always set as Variant. This is generally like blood type O in that it works everywhere - more or less. There are a few standard procedures that may have changed from ByVals to ByRefs which would cause it to work happily before the change but not afterwards. I'm 99(.999999)% sure that there has never been a time when setting any one of a list of variables to a specific type would cause any of the others to be set that way too.

To be absolutely clear :
Expand|Select|Wrap|Line Numbers
  1. Dim A, B, C AS Integer
Has always resulted in :
Expand|Select|Wrap|Line Numbers
  1. A ==> Variant
  2. B ==> Variant
  3. C ==> Integer
Nov 19 '15 #16
zmbd
5,501 Expert Mod 4TB
IDK either... the typecasting solved the issue.
Nov 19 '15 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Colm O'Brien | last post by:
I have two tables and need help with a query Table 1 is a lookup table fields monthno 1-12 and month name January- December Table 2 lots of fields but 2 important to this query yearendmonth which...
10
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only...
0
by: hozzyk | last post by:
I designed a blog template, but i can't get it to look right. I created a shadow, like a drop shadow, but on all sides of the table. I created it as a PNG for transparency. Here is the problem,...
1
by: newtechiebug | last post by:
I am trying to convert a MS Access Query to a SQL query and I need help with some IIF Statements? 2 in particular that I've encountered problems with are: Count(IIf(=0 Or...
3
by: Dorish3 | last post by:
Hello, I am a novice with MS Access so I want to apologize ahead of time with the fact that I know very little about Access. I have an Access database that keeps track of volunteers and...
6
by: svgeorge | last post by:
I need help in C Sharp Web Interface , I have web pages for making several 9 type of payments. The data gets loaded on web page from SQL server 2005 database. Then I have Process payment...
1
by: Iyhamid | last post by:
Hello Every 1: I Have this query: Tran Date Total Tran Per Day 200 0000 4 0710 38 0711 78 0712 15
4
by: SBmx | last post by:
hello I need help building a SELECT query... well, two queries actually i have three tables: products / fields id, name, type, country producttypes / fields: id, name countries / fields:...
3
by: AustinGoldfish | last post by:
I am trying to change a query to access different tables each time the query is run. ***I know, I know, there are other ways to accomplish this. However, this is what I must to do in order to...
9
by: phil28307 | last post by:
I cannot get my query to order the months in correct order. Here is my SQL. SELECT DISTINCTROW Format$(.,'mmmm yyyy') AS , Avg(.) AS , Avg(.) AS , Avg(.) AS , Avg(.) AS FROM GROUP BY...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.