Results 1 to 8 of 8

Thread: Code suggestions [Resolved]

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    10

    Resolved 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:
    1. Sub Macro2()
    2.  
    3.     With ActiveSheet.QueryTables.Add(Connection:= _
    4.         "FINDER;C:\test\Query from MS Access Database.dqy" _
    5.         , Destination:=Range("A1"))
    6.         .Name = "Query from MS Access Database"
    7.         .FieldNames = True
    8.         .RowNumbers = False
    9.         .FillAdjacentFormulas = False
    10.         .PreserveFormatting = True
    11.         .RefreshOnFileOpen = False
    12.         .BackgroundQuery = True
    13.         .RefreshStyle = xlInsertDeleteCells
    14.         .SavePassword = True
    15.         .SaveData = True
    16.         .AdjustColumnWidth = True
    17.         .RefreshPeriod = 0
    18.         .PreserveColumnInfo = True
    19.         .Refresh BackgroundQuery:=False
    20.        
    21.     End With
    22.     Application.Goto Reference:="Macro2"
    23. End Sub
    Last edited by hatemachine; Jan 30th, 2005 at 08:12 PM. Reason: Erased line in code

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    10

    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:
    1. ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row

    Got that from here
    Last edited by hatemachine; Jan 30th, 2005 at 05:17 PM. Reason: word

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    10

    Re: Code suggestions

    I tried replacing this line:
    VB Code:
    1. (Connection:= _
    2.         "FINDER;C:\test\Query from MS Access Database.dqy" _
    3.         , Destination:=Range("A1"))
    with
    VB Code:
    1. (Connection:= _
    2.         "FINDER;C:\test\Query from MS Access Database.dqy" _
    3.         , 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?

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Dim sCell as string
    2. sCell = "A" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
    3. (Connection:= _
    4.         "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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    10

    Resolved Re: Code suggestions

    Thanks... Problem Solved

    VB Code:
    1. Sub Macro2()
    2.     Dim sCell As String
    3.     sCell = "A" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row + 1
    4.  
    5.     With ActiveSheet.QueryTables.Add(Connection:= _
    6.         "FINDER;C:\test\Query from MS Access Database.dqy" _
    7.         , Destination:=Range(sCell))
    8.         .Name = "Query from MS Access Database"
    9.         .FieldNames = False
    10.         .RowNumbers = False
    11.         .FillAdjacentFormulas = False
    12.         .PreserveFormatting = True
    13.         .RefreshOnFileOpen = False
    14.         .BackgroundQuery = True
    15.         .RefreshStyle = xlInsertDeleteCells
    16.         .SavePassword = True
    17.         .SaveData = True
    18.         .AdjustColumnWidth = True
    19.         .RefreshPeriod = 0
    20.         .PreserveColumnInfo = True
    21.         .Refresh BackgroundQuery:=False
    22.     End With
    23.     Cells.Select
    24.     Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
    25.         OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    26.         DataOption1:=xlSortNormal
    27. 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.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width