|
-
Jan 30th, 2005, 03:02 PM
#1
Thread Starter
New Member
Code suggestions [Resolved]
First of all, I looked at VB for the first time just Thursday, so excuse my ignorance. If you have any links to good tutorial sites, that would be great. Until then, here is my question. I got my VB code by recording a macro. It imports a search query and displays the results to cell A1. I would like it to display into the first row with no data in it though. I'm not sure how to do this. Right now, my query asks for a certain database number, so if I have to import multiple database numbers to the same sheet, I have to run it three times. But with this code, it will overwrite the previous database. After I figure this out, I'll be working on looping this thing for a certain amount of times. But for the time being, just not erasing the data would be better than what I'm having to do now.
VB Code:
Sub Macro2()
With ActiveSheet.QueryTables.Add(Connection:= _
"FINDER;C:\test\Query from MS Access Database.dqy" _
, Destination:=Range("A1"))
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Application.Goto Reference:="Macro2"
End Sub
Last edited by hatemachine; Jan 30th, 2005 at 08:12 PM.
Reason: Erased line in code
-
Jan 30th, 2005, 03:49 PM
#2
Re: Code suggestions
Welcome to the Forums.
I'm a little unclear on your question. You say that you want to place the non
data in a row "I would like it to display into the first row with no data in it though."
Also, when it places the data in A1 for the first loop, where do you want the
next data placed.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 30th, 2005, 05:08 PM
#3
Thread Starter
New Member
Re: Code suggestions
Oh... sorry about that. So let's say I have a blank spreadsheet. I want the data to be placed in A1. After that, the data may take up 3 rows (but it varies). So the next loop, I want it in cell A4 (for this example). If I re run the macro as is, it will replace the data in A1 instead of appending it to the next available row. Does that make sense?
So I need to figure out how to put the data into the first row in my spreadsheet that has no data in it. I read somewhere that I need to use something like this:
VB Code:
ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
Got that from here
Last edited by hatemachine; Jan 30th, 2005 at 05:17 PM.
Reason: word
-
Jan 30th, 2005, 06:20 PM
#4
Re: Code suggestions
Yes that is the code you need to place the query results in the next available row.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 30th, 2005, 06:37 PM
#5
Thread Starter
New Member
Re: Code suggestions
I tried replacing this line:
VB Code:
(Connection:= _
"FINDER;C:\test\Query from MS Access Database.dqy" _
, Destination:=Range("A1"))
with
VB Code:
(Connection:= _
"FINDER;C:\test\Query from MS Access Database.dqy" _
, Destination:=ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row)
That did not work. And I would guess it has to do with my lack of programming knowledge as to why it did not work. Do I need some other function to use this?
-
Jan 30th, 2005, 06:50 PM
#6
Re: Code suggestions
Since your query is returning a single field value, you need to specify the cell
(range) for your query results to be placed into, not just an entire row.
VB Code:
Dim sCell as string
sCell = "A" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
(Connection:= _
"FINDER;C:\test\Query from MS Access Database.dqy", Destination:=sCell) 'A53 for example
HTH
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 30th, 2005, 08:05 PM
#7
Thread Starter
New Member
Re: Code suggestions
Thanks... Problem Solved
VB Code:
Sub Macro2()
Dim sCell As String
sCell = "A" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row + 1
With ActiveSheet.QueryTables.Add(Connection:= _
"FINDER;C:\test\Query from MS Access Database.dqy" _
, Destination:=Range(sCell))
.Name = "Query from MS Access Database"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Cells.Select
Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub
Now trying to figure out how to edit the original subject to resolved.
Last edited by hatemachine; Jan 30th, 2005 at 09:57 PM.
-
Jan 30th, 2005, 09:54 PM
#8
Re: Code suggestions [Resolved]
No prob. Glad to help.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|