Results 1 to 6 of 6
  1. #1
    kadoss is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2017
    Posts
    4

    Prevent table export if table name already exists

    Hi All - I hope someone can help. I found some code via the internet and modified it slightly and need some help with additional modifications. I've tried various internet searches and searching on this forum, but am appearently not searching the correct words/phrases to come up with what I am looking for.

    I have 2 databases, db1 is my local database and db2 is my archive database.

    db1 has a form with 4 combo boxes and a button with code that takes the info from the combo boxes and uses it to name a table that is exported from db1 into db2. The code then deletes the records from db1. The problem is that if people accidently name the tables the same, it will just copy over the existing archived copy.

    So, I need the code to check to see if the table name already exists in db2 (the archive database). If that name already exists - it needs to pop up with a warning and NOT export the table. Below is the current code that I have. Any help would be greatly appreciated.
    Sub New_Table()
    Dim MBox As String, YBox As String, NBox AsString, IBox As String
    On Error GoTo Err_ErrorHandler

    strM = Forms("frm_Export").MBox.Value
    strY = Forms("frm_Export").YBox.Value
    strN = Forms("frm_Export").NBox.Value
    strI = Forms("frm_Export").IBox.Value
    strT = strM & "_" & strY &"_" & strN & "_" & strI

    If IsNull(strM) Then
    MsgBox"You must enter data in ALL fields to name the table you arearchiving!", , "Error"
    Exit Sub
    Else
    DoCmd.TransferDatabase acExport, "Microsoft Access", "L:\Path\To\Archived\Database.accdb",acTable, "tbl_MyFindingsTable", "tbl_" & strT


    DoCmd.SetWarnings False
    DoCmd.RunSQL "DELETE * FROM [tbl_MyFindingsTable]"
    DoCmd.SetWarnings True
    MsgBox"The table has been archived."

    End If

    Exit_ErrorHandler:
    strT =vbNullString
    Exit Sub

    Err_ErrorHandler:
    MsgBoxErr.Description, vbExclamation, "Error #" & Err.Number
    ResumeExit_ErrorHandler

    End Sub

  2. #2
    Bulzie is online now VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    How about addi the datetime tag to the end of the file name so they are always unique?

    strT = strM & "_" & strY &"_" & strN & "_" & strI & "_" & Format(Now(), "mmddyyyyhhmmss")

  3. #3
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Any use to you?

    https://access-programmers.co.uk/for...d.php?t=154842




    Sent from my iPhone using Tapatalk

  4. #4
    kadoss is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2017
    Posts
    4
    Quote Originally Posted by Bulzie View Post
    How about addi the datetime tag to the end of the file name so they are always unique?

    strT = strM & "_" & strY &"_" & strN & "_" & strI & "_" & Format(Now(), "mmddyyyyhhmmss")
    Bulzie - Thanks for the quick response! This would definately keep the original table from being overwritten, and would be able to tell which was saved first. And having the date/time might be helpful. Might see if there are any other helpful suggestions, but this may be the way to go.

  5. #5
    kadoss is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2017
    Posts
    4
    Hey Andy49 - I appreciate the link. It probably would be of use if I was a little bit more code savy than I am.
    I may just go with adding the date/time stamp option for simplicity's sake.

  6. #6
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Heh always add the easy option.


    Sent from my iPhone using Tapatalk

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 01-18-2017, 10:29 AM
  2. Replies: 1
    Last Post: 03-31-2016, 03:33 AM
  3. Replies: 2
    Last Post: 01-31-2016, 08:47 PM
  4. Replies: 2
    Last Post: 10-09-2012, 03:20 PM
  5. If table exists then
    By SorenIX in forum Programming
    Replies: 2
    Last Post: 06-26-2011, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums