madmix
May 3 2012, 09:31 AM
Hi All,
I am working on a database where users can browse to an excel spreadsheet and import the data from that spreadsheet into a table. I have added a field to this table (not on the spreadsheet) called fldNum. What I am trying to do is set it up so that on import, this field will be populated automatically with an auto-incrementing number that increments by 4's. I have searched and pieced together from this forum some great help on making the auto-increment and import / transferspreadsheet code but I don't quite know how to put this all together. I am pretty sure I will need a loop of some sort but I am not quite there yet. Any help is appreciated. Here is what I have so far:
Import code:
CODE
Private Sub cmdImportMetadata_Click()
Dim sFile As FileDialog ' Windows File browser
Dim Selection As String
' Set variable
' For a file picker
Set sFile = Application.FileDialog(msoFileDialogFilePicker)
' For a folder picker
'Set sFile = Application.FileDialog(msoFileDialogFolderPicker)
' Open and check for CANCEL
With sFile
.AllowMultiSelect = False
' .Filters.Add "Excel Files", "*.xls; *.xlsx; *.csv; *.txt;", 1
If .Show <> -1 Then
Exit Sub
End If
End With
' Store full path
Selection = sFile.SelectedItems(1)
'Import selected file to table
DoCmd.TransferSpreadsheet acImport, 10, "tblMetadata", Selection, True, ""
' Release variable
Set sFile = Nothing
Exit_BrowseSource:
'Refresh
Exit Sub
Err_BrowseSource:
MsgBox "Error in Browse: " & Err.Description
Resume Exit_BrowseSource
End Sub
Auto-increment code:
FYI... With this I am able to increment a single field in another table that has only one record (not quite what i am after but it's a start :-)
CODE
Public Function AutoIncrement()
Dim sTable As String
Dim rs As DAO.Recordset
Dim NextNum As Long
Dim iCounter As Integer
iCounter = 0
NextNum = 0
sTable = "tblANumAssets"
Set rs = CurrentDb.OpenRecordset(sTable, dbOpenDynaset, dbDenyWrite)
If rs.RecordCount = 0 Then
'no current records... start at 1
NextNum = 1
'update the ANnum Table
With rs
rs.AddNew
rs(0) = NextNum
.Update
End With
Else 'there's an existing record
'current record availble, increment by 4
rs.MoveFirst
NextNum = rs(0) + 4
'update the ANnum Table
With rs
.Edit
rs(0) = NextNum
.Update
End With
End If
'close rs
rs.Close
Set rs = Nothing
End Function
theDBguy
May 3 2012, 09:49 AM
Hi,
What version of Access are you using? Please remember to select the version number when posting questions in case it becomes relevant to the discussion.
How many records (max) are we talking about?
Just my 2 cents...
fkegley
May 3 2012, 09:53 AM
You should be able to do this in one block of code:
Once the spreadsheet has been imported into the table,
1) Open the imported table into a recordset
2) Open AutoNumber table into a recordset
3) Assign the AutoNumber value to the imported table recordset
4) Update the imported table recordset
4) Update the AutoNumber to its next value
5) Move to next record in imported table recordset
madmix
May 3 2012, 09:58 AM
Hmmm. I guess that bug you mentioned in an earliere thread really exists. I paid particular attention to select my Access version this time (it's 2010).
Thanks,
Ken
madmix
May 3 2012, 10:01 AM
Thanks, Frank for your reply. I will see about how to code this sequence. BTW, do you thin it's possible to eliminate the AutoNumber table and just do it using the imported table?
Thanks,
Ken
theDBguy
May 3 2012, 10:07 AM
Hi Ken,
Does the import always add records to an empty table, or does it continue to add them with existing records?
MadPiet
May 3 2012, 10:31 AM
Unless I'm misunderstanding something, you should be able to get the last (MAX) autonumber, and increment from there. Should be simple - execute a totals query (MAX/LAST) and then grab the 'autonumber' column value, stuff it into a variable, and then in your incrementing code, count off by 4....
madmix
May 3 2012, 10:40 AM
QUOTE (theDBguy @ May 3 2012, 03:07 PM)

Hi Ken,
Does the import always add records to an empty table, or does it continue to add them with existing records?
It will add to existing records.
QUOTE (MadPiet @ May 3 2012, 03:31 PM)

Unless I'm misunderstanding something, you should be able to get the last (MAX) autonumber, and increment from there. Should be simple - execute a totals query (MAX/LAST) and then grab the 'autonumber' column value, stuff it into a variable, and then in your incrementing code, count off by 4....
I am thinking this would happen in a for... each loop?
Ken
theDBguy
May 3 2012, 10:50 AM
Hi Ken,
QUOTE (madmix @ May 3 2012, 08:40 AM)

It will add to existing records.
Thanks. Sorry to keep asking questions; I am trying to figure out if it's possible to do this without using code.
What is the purpose of the "incrementing" number? Are they going to be permanently assigned to each specific record?
Just my 2 cents...
madmix
May 3 2012, 11:05 AM
QUOTE (theDBguy @ May 3 2012, 03:50 PM)

Hi Ken,
Thanks. Sorry to keep asking questions; I am trying to figure out if it's possible to do this without using code.
What is the purpose of the "incrementing" number? Are they going to be permanently assigned to each specific record?
Just my 2 cents...

No problem and thanks for your help. The numbers will be used in a query to generate unique media ID numbers for media assets. Each asset needs 4 unique numbers (hence the increment of 4). For example my query expressions to generate the numbers for one asset will be something like:
"HALO" & [fldNum] + 1000000000000000
"HALO" & [fldNum] + 1000000000000001
"HALO" & [fldNum] + 1000000000000002
"HALO" & [fldNum] + 1000000000000003
Kind regards,
Ken
theDBguy
May 3 2012, 11:21 AM
Hi Ken,
I think you could try something like this:
1. Create a table (tblMediaIDs) with four records in it. For example:
ID MediaID
1 1000000000000000
2 1000000000000001
3 1000000000000002
4 1000000000000003
2. Add an Autonumber field in your import table.
3. Create a query to generate the MediaIDs. For example:
SELECT "HALO" & (([AutonumberField] * 4) + [MediaID]) As NewMediaID
FROM ImportTable, tblMediaIDs
(untested)
Just my 2 cents...
madmix
May 3 2012, 11:48 AM
theDBguy,
That sound really good! I would be happy to avoid having to use code. I have to run to a meeting in a few but I will try it when I return and post back.
Kind regards,
Ken
fkegley
May 3 2012, 12:22 PM
That would depend on what you are doing to the existing table before you import the next batch. If keeping the existing data, then appending the new data, obviously you will need to keep the next value.
If you import into an empty table each time, then you could just build it into the code, without using a table.
madmix
May 3 2012, 01:32 PM
Hi theDBguy,
I just tried this but I still got a sequence that incremented by 1. The records need to be numbered in increments of 4, like:
1
5
9
13
and so on.
I might not have done it right but I will keep working on this... For now, more meetings.
Ken
theDBguy
May 3 2012, 01:41 PM
Hi Ken,
From what I understand, you only wanted to increment the imported records by 4 to produce 4 media IDs. If so, that is why I multiply the Autonumber by 4 in the query I suggested. Which means I was expecting it to increment by one.
madmix
May 3 2012, 02:09 PM
QUOTE (theDBguy @ May 3 2012, 06:41 PM)

Hi Ken,
From what I understand, you only wanted to increment the imported records by 4 to produce 4 media IDs. If so, that is why I multiply the Autonumber by 4 in the query I suggested. Which means I was expecting it to increment by one.
Ahhh, my bad. I must not have communicated it properly.
theDBguy
May 3 2012, 02:17 PM
QUOTE (madmix @ May 3 2012, 12:09 PM)

Ahhh, my bad. I must not have communicated it properly.
So, are you saying that it's still essential to increment the number by 4? Why?
madmix
May 3 2012, 02:53 PM
QUOTE (theDBguy @ May 3 2012, 07:17 PM)

So, are you saying that it's still essential to increment the number by 4? Why?

Yes, I still need to increment by 4. Each record needs to have 4 unique media id's. If I assign these id's incrementally by 1's then the first media ID in the next record will have to begin 4 higher than the first record in the previous one.
Kind regards,
Ken
madmix
May 3 2012, 02:53 PM
I think I am on to something here but I am having trouble with the If Then statement (I am also not sure my With statement is correct). Hopefully you can see what I am going for. Please help
Thanks in advace!
CODE
Public Function AutoIncrement()
Dim sTable As String
Dim dTable As String
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset2
Dim NextNum As Long
NextNum = 0
sTable = "tblANumAssets"
dTable = "tblMetadata"
Set rs1 = CurrentDb.OpenRecordset(sTable, dbOpenDynaset, dbDenyWrite)
Set rs2 = CurrentDb.OpenRecordset(dTable, dbOpenDynaset, dbDenyWrite)
With rs2
Do While Not .EOF
'INSERT IF STATEMENT HERE TO CHECK FOR NULL fldNum FIELDS IN tblMetadata
'If IsNull [fldNum] then
If rs1.RecordCount = 0 Then
'no current records... start at 1
NextNum = 1
'update the tblANumAssets Table
With rs1
rs.AddNew
rs(0) = NextNum
.Update
End With
Else 'there's an existing record
'current record availble, increment by 4
rs1.MoveFirst
NextNum = rs1(0) + 4
'update the tblANumAssets Table
With rs1
.Edit
rs1(0) = NextNum
.Update
End With
End If
'UPDATE fldNum FIELD IN tblMetadata WITH THE NextNum VALUE
'[fldNum] = NextNum
.MoveNext
Loop
.Close
End If
End With
rs1.Close
Set rs1 = Nothing
rs2.Close
Set rs2 = Nothing
End Function
theDBguy
May 3 2012, 06:17 PM
Hi Ken,
QUOTE (madmix @ May 3 2012, 12:53 PM)

Yes, I still need to increment by 4. Each record needs to have 4 unique media id's. If I assign these id's incrementally by 1's then the first media ID in the next record will have to begin 4 higher than the first record in the previous one.
Kind regards,
Ken
But that's exactly what my query should be doing for you. Although the Autonumber field is incremented by one, the MediaIDs will be unique for all records (and incremented by 4 because I multiply the Autonumber field by 4). Does it not do that for you?
Sorry, I'm still confused as to what end product you're really looking for. You will no doubt eventually find the solution using code. But, I just thought that using the query I gave just avoids doing the same thing using code.
Just my 2 cents...
madmix
May 3 2012, 09:42 PM
QUOTE (theDBguy @ May 3 2012, 11:17 PM)

Hi Ken,
But that's exactly what my query should be doing for you. Although the Autonumber field is incremented by one, the MediaIDs will be unique for all records (and incremented by 4 because I multiply the Autonumber field by 4). Does it not do that for you?
Sorry, I'm still confused as to what end product you're really looking for. You will no doubt eventually find the solution using code. But, I just thought that using the query I gave just avoids doing the same thing using code.
Just my 2 cents...

I totaly agree with you theDBguy. I would rather not use code if I can accomplish this with a query. I might have implemented your solution incorrectly. One thing I did notice is that since the media ID number has so many digits (16 total) that the only way I could get it to work was to set the type property to Double or text in the table field. Then it came up as some sort of "scientific" representation in my query. Let me work with it some more when I get into the office and I will post back on my results.
I attached a spreadsheet that I am currently using for this task (a bit klunky but it gets the job done). If you unhide the columns between P & AG you will see "SD Title Asset", SD Movie Asset" etc... These fields use the value from column AG in a formula. Now look at how column AG is incrementing from record to record. That's what I need to reproduce in Access.
Thanks for hangin' in there with me!
Ken
Click to view attachment
madmix
May 4 2012, 07:44 AM
theDBguy,
OMG I can't believe it was soooo simple!!! You are correct. and I was able to do it without the [fldNum] field in my main table or having any additional table. Once I thought about your idea a little bit, it made sense! I used the [ID] field in my main table and applied the same principal from your idea and it worked perfectly! Sorry I was so dense before.
You rock!
Ken
theDBguy
May 4 2012, 09:25 AM
Hi Ken,
Glad to hear you got it to work. We are all happy to help.
Good luck with your project.
rajibulhuda
May 9 2012, 07:11 AM
First import the xl file to a temporary table, then use loop for .... each and execute the insert into statement with conditions and delete the temporary table.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.