Yes, all code is in 1 Private Sub.
There is a column in the table which contains a record number. It is a number data type in which I can manipulate and renumber in order to change it's position in the ListBox on the form. Here is the code being used to open, copy and renumber if necessary. Thanks for helping me.
'All code below on Form frm_Start
Option Compare Database: Dim Line, Auto, Records As Integer: Dim SQLa, strFolderName As String
Dim conn As ADODB.Connection: Dim rs As ADODB.Recordset
Private Sub Filename_AfterUpdate()
'Drop old tables before import and copy
DoCmd.RunSQL "DROP TABLE tbl_Copy": DoCmd.RunSQL "DROP TABLE ABS_Daily"
'Import file into ABS_Daily based on selected Filename here
Path = "C:\Program Files\DailyABS\" & [Filename] & ".mdb"
DoCmd.TransferDatabase acImport, "Microsoft Access", Path, acTable, "ABS_Daily", "ABS_Daily", False
Execute = "C:\Windows\System32\Cmd.exe /C "
'Copy ABS_Daily table to new table tbl_Copy
DoCmd.CopyObject , "tbl_Copy", acTable, "ABS_Daily"
'Get line number from Scelestus program
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Scelestus\Scelestus.mdb" & ";" & _
"Persist Security Info=False": conn.Open: SQLa = "SELECT Line from ([tbl line index])"
Set rs = conn.Execute(SQLa, , adCmdText): Line = rs.Fields("Line").Value
rs.Close: Set rs = Nothing: conn.Close: Set conn = Nothing
'Delete all records but associated line for use with above from Copy
DoCmd.RunSQL "DELETE * from tbl_Copy Where Line <> " & Line
'Determine if Auto has data, if so then it has been edited, skip numbering
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\DailyABS\OrderSpec.mdb" & ";" & _
"Persist Security Info=False": conn.Open: SQLa = "SELECT Sum(Auto) as Auto from tbl_Copy"
Set rs = conn.Execute(SQLa, , adCmdText): Status = rs.Fields("Auto").Value
SQLa = "SELECT Count(Material) as Records from tbl_Copy"
Set rs = conn.Execute(SQLa, , adCmdText): Records = rs.Fields("Records").Value
'Check for Auto to match records
If Status > 0 Then
Again:
SQLa = "SELECT Auto from tbl_Copy where Auto = " & [Records]
Set rs = conn.Execute(SQLa, , adCmdText)
If rs.EOF Then
SQLa = "SELECT Count(Auto) as MT from tbl_Copy Where Auto = 0"
Set rs = conn.Execute(SQLa, , adCmdText): MT = rs.Fields("MT"): MT = MT - 1: RecNew = [Records] - MT
SQLa = "SELECT Distinct Material, Plant, [Target Quantity],Auto from tbl_Copy Where Auto = 0"
Set rs = conn.Execute(SQLa, , adCmdText)
M = rs.Fields("Material"): Pl = rs.Fields("Plant"): T = rs.Fields("Target Quantity")
SQLa = "Update tbl_Copy Set Auto = " & RecNew & " Where Material = '" & M & "' and Plant = '" & Pl & "' and [Target Quantity] = " & T & ""
Set rs = conn.Execute(SQLa, , adCmdText)
End If
If MT <> 0 Then GoTo Again:
Set rs = Nothing: conn.Close: Set conn = Nothing: GoTo Skip:
End If
rs.Close: conn.Close: Set rs = Nothing: Set conn = Nothing
'If Auto has no data then it is an original copy. Number all remaining records left after delete from Copy
Set WS = DBEngine.Workspaces(0): Set MyDb = WS.OpenDatabase("C:\Program Files\DailyABS\OrderSpec.mdb")
Set MYSet = MyDb.OpenRecordset("tbl_Copy"): Auto = 1
MYSet.MoveFirst: Do Until MYSet.EOF: MYSet.Edit: MYSet("Auto") = Auto
MYSet.Update: Auto = Auto + 1: MYSet.MoveNext: Loop
MYSet.Close: MyDb.Close: WS.Close: Set WS = Nothing: Set MYSet = Nothing: Set MyDb = Nothing
Skip:
DoCmd.OpenForm "frm_Schedule", , , stLinkCriteria
'Transfer data to frm_Schedule before closing frm_Start, all data manipulation complete
[Forms]![frm_Schedule]![Line] = [Line]: [Forms]![frm_Schedule]![Records] = [Records]: [Forms]![frm_Schedule]![Filename] = [Filename]
[Forms]![frm_Schedule]![Text501] = [Text501]: [Forms]![frm_Schedule]![Text502] = [Text502]: [Forms]![frm_Schedule]![Text503] = [Text503]
[Forms]![frm_Schedule]![Text504] = [Text504]: [Forms]![frm_Schedule]![Text505] = [Text505]: [Forms]![frm_Schedule]![Text506] = [Text506]
[Forms]![frm_Schedule]![Text441] = [Text441]: [Forms]![frm_Schedule]![Text475] = [Text475]: [Forms]![frm_Schedule]![TextQty] = [TextQty]
DoCmd.Close acForm, "frm_Start": DoCmd.SetWarnings True
End Sub