primary key autonumbering

ramasterre

Active Member
Joined
Oct 5, 2004
Messages
253
I am working in an Access database that I did not create but I am now in charge of updating some of the stuff we do with it. I am wondering, one of the tables has a primary key that is basically a 'case' number made of the date and a 5 digit number. It basically takes the date and auto generates the primary keys. This works fine for the userform that populates the table. However, I want to (from time to time) be able to dump data from an excel document into the table instead of using the form. Is there a way to get the primary key to continue to autopopulate without using the user form?

any help would really be appreciated.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Here is an idea that may or may not help.

I would look at importing it into a temporary table. Then use and Update Query to populate the primary values you need, and then use and Append Query to write the information back to your main table.

You could probably also do all this with some high level VBA as you are importing the data all in one step, but I am not quite that advanced in VBA yet.
 
Upvote 0
hmm.. sound like i just may have to go with that. Thanks for the suggestion.


Does anyone know of any other way?
 
Upvote 0
Hi
I presume there is a macro or VBA event that runs when either a record is saved or the form is closed. Can you determine how the unique values are populated from within the form? If so, can you replicate that process in another macro as part of the import process?

If you aren't having much luck with an automated process then what is the format and default value of this particular field in the table design screen? You might also want to check the default value in the form design screen.
Andrew :)
 
Upvote 0
I have located the macro for the autonumbering but quite frankly i dont understand it.

Code:
Function CreateStamp(f As Form)
    ' This function will take the name of a table and the name of the
    ' field that is the primary key.  It will increment a counter, then
    ' assign a unique key
    On Error Resume Next
    Dim db As Database, rst As Recordset
    Dim strReturn As String, intYear As String
    Dim intMonth As String, intDay As String
    Dim intI As Integer
    Dim dtRightNow As Date
    
    strReturn = ""
    Set db = CurrentDb()
    Set rst = db.OpenRecordset("tblCaseCount")
    
    'Move to the last record in tblCase Count and get value
    rst.MoveLast
    intI = rst!Count.Value
    
    dtRightNow = Now()
    intYear = Format(dtRightNow, "yyyy")
    intMonth = Format(dtRightNow, "mm")
    intDay = Format(dtRightNow, "dd")
    strReturn = intYear & intMonth & intDay & Trim(CStr(intI))
    
    With rst
        .AddNew
        !CaseType = f.CASE_TYPE
        .Update
    End With
    rst.MoveLast
    intI = rst!Count.Value
    strReturn = intYear & intMonth & intDay & Trim(CStr(intI))
    f.CASE_NO.Value = strReturn
    
    'Close and reset objects
    db.CLOSE
    Set db = Nothing
    rst.CLOSE
    
End Function


can i do anything with this? or is my only choice to rig somedthing with an append query ?

thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,221,904
Messages
6,162,744
Members
451,785
Latest member
DanielCorn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top