+ Reply to Thread
Results 1 to 12 of 12

From Excel, display MsgBox with ID from Access table

  1. #1
    Registered User
    Join Date
    02-08-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    21

    From Excel, display MsgBox with ID from Access table

    I would like to put together a macro in Excel that looks at an Access table and displays a MsgBox with the last(highest) ID number from said table.

    The Excel spreadsheet would be located on end users computers in various locations.

    The Access database is located at: "D:\Users\Henry\Desktop\Tracking.accdb" from all computers.
    The table name is: "Tbl-Form" and the field name is "ID".

    If you can provide pointers to examples, I would appreciate it.

    Thanks in advance!!

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,894

    Re: From Excel, display MsgBox with ID from Access table

    You can use ADO to query database.

    See link for example.
    http://www.minnesotaithub.com/2014/0...with-vbscript/

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: From Excel, display MsgBox with ID from Access table

    I am on a rush. I have tested it and works on old the sample I had created for you. It puts the value in to Range A2.
    Please Login or Register  to view this content.
    Last edited by AB33; 04-17-2017 at 12:55 PM.

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: From Excel, display MsgBox with ID from Access table

    Just like looping through the rows to find the last row, it is not really good practise to open a table and loop to the last record as in the example.

    Better to sort the table descending by ID and select the TOP 1 record. Your real DBA will appreciate it if this gets moves to an serious DB

    Please Login or Register  to view this content.
    There's a sample DB in the attachment - extract to the same directory as the workbook this is test with. Remembering, if you open a new workbook it must be saved first as 'Application.Path' (used in the code) is empty until the WB has been saved.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    02-08-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    21

    Re: From Excel, display MsgBox with ID from Access table

    CK76
    Thanks for jumping in and giving me a hand!
    I've modified the script you pointed out with my information, but only need the highest or last ID number entered.
    I don't believe I need the looping, but don't know enough about it to be sure.

    While using this code, I initially received the following error.

    runtime error '-2147217900 (80040e14)'
    On this line: Set rs = objConn.Execute("SELECT [ID] FROM Tbl-Form")

    I placed Tbl-Form in brackets as [Tbl-Form] and am no longer receiving an error.

    I have a msgbox popping up, but instead of displaying the last ID which should be TR0684, it says 228340.

    Any thoughts on what that might be?





    AB33
    I certainly appreciate you popping in here!
    I didn't want to add additional requests for something different to my other post, so I started this one.

    In this case, I am not providing the ID number to Access, it is just incrementing automatically perfectly.
    I also do not want the ID number from Access returned to a cell within Excel.

    I would like a msgbox to open in Excel and display the full ID number from Access.

    Ex: ID# TR0686

    If I have misunderstood, I apologize.

    What you gave me works great to return it to cell A2, but I am looking for a MsgBox.





    cytop
    Thanks for responding, but there are pieces of yours that I do not understand.
    I am unable to download your example and apologize if the answer is inside.

    For strPath you have \Database11.accdb
    My database is at D:\Users\Henry\Desktop\Tracking.accdb
    Would I just substitute it here?

    At strConnection I assume I put my database location here at Data Source, correct?

    I am just looking for the last ID entered, the highest in the series.
    Do I need to loop or does the looping start at 1 and cycle through all numbers entil the last number?

    Thank you so much for responding!

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: From Excel, display MsgBox with ID from Access table

    Set strPath = the full path to your database. If it is a .ACCDB then that is the only change to be made. IF a .MDB file then you'll need to change the connection string.

    The query assumes a table in the database named 'TABLE1' and the ID field named 'ID'. Change any reference to these to suit your actual data structure.

    There is no looping. The function gets the last/highest used number in the ID field, adds 1 and returns that and is about as optimised as it can be. Adding an index (or Key) to the ID field would also help - but an ID field should already be a primary key in any case.

    If this is a numeric ID field, it begs the question why it is not an AutoNumber field.
    Last edited by cytop; 04-17-2017 at 03:38 PM.

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,894

    Re: From Excel, display MsgBox with ID from Access table

    @elzool
    Any thoughts on what that might be?
    Not sure as I don't know what's in your DB. But as cytop indicated, you should sort query based on some field (be it ID or time stamp) and return Top 1 for [ID].
    What you did was just grab everything from that [ID] column and getting first value in recordset.

  8. #8
    Registered User
    Join Date
    02-08-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    21

    Re: From Excel, display MsgBox with ID from Access table

    cytop
    Thank you for the response!

    For strPath I have inserted the path to the database.
    Would I leave strConnection as it is or change it to the same path?

    As it is now, I am receiving an error on con.Open strConnection of Not a valid file name.

    This is what I have now.
    Please Login or Register  to view this content.




    ck76
    Gotcha, I will start working on that then. Thank you

  9. #9
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,894

    Re: From Excel, display MsgBox with ID from Access table

    This line.
    Please Login or Register  to view this content.
    You already hard coded path. But you are concatenating strPath on top of it.

  10. #10
    Registered User
    Join Date
    02-08-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    21

    Re: From Excel, display MsgBox with ID from Access table

    I ended up up going with the example from AB33.
    Thanks yet again AB33!!

    I just added a line to display the MsgBox information.

    Thanks for the assistance everyone!

    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: From Excel, display MsgBox with ID from Access table

    elzool,
    Sorry!
    I left you in limbo. I was about to turn-off my PC when I saw the thread.
    Here is another option-a one liner.

    MsgBox ("ID No is " & rs.GetString())

  12. #12
    Registered User
    Join Date
    02-08-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    21

    Re: From Excel, display MsgBox with ID from Access table

    AB33

    That was perfect, thank you yet again!
    Less is more and is quite elegant.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA to Display Table row & line in MsgBox
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-26-2015, 11:28 PM
  2. From Excel run make table query in Access and show msgbox
    By morsoe in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2013, 10:32 AM
  3. [SOLVED] VB evaluate a value in a table's column and display msgbox
    By AusTexRich in forum Excel General
    Replies: 8
    Last Post: 10-10-2005, 06:05 PM
  4. [SOLVED] Msgbox in Excel from Access (Correct)
    By Picos Autom in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-03-2005, 11:05 PM
  5. MsgBox in Access Automate Excel
    By Picos Autom in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-03-2005, 11:05 PM
  6. Msgbox in Excel from Access
    By Picos Autom in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-03-2005, 05:05 PM

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