Discussion Locked

VB 4 Access double points to 1st right

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

If you can email me I will give you a more detailed explanation, but here is my problem. I have a table in access, two of the columns change numbers every few records. eg.
first last add1 add2 1 1
first last add1 add2 1 1
first last add1 add21 2
first last add1 add2 1 2
first last add1 add2 2 1
first last add1 add2 2 1
first last add1 add2 2 1
first last add1 add2 2 2
ect... with the two columns,of just numbers,
I need to have a VB funtion that will copy each recordto a new table and enter a new record right before the record that has the changed number.It doesn't matter which column changes , it would have the same effect. In this new record I am wanting to have all of the fields blank, except I am trying to put the "first" field in the "add1" column and the "last" in the "add2" column. So basically it would look like this:
first last add1 add2 1 1
first last add1 add2 1 1
first last
first last add1 add2 1 2
first last add1 add21 2
first last
first last add1 add2 2 1
first last add1 add2 2 1
first last add1 add2 2 1
first last
first last add1 add2 2 2

I would so appreciate if some VB expert could help me I have been trying to research this and just can't figureout how to get it to work PLeeaassee help will give 2000 extra to someone who could do it before monday at 10 a, PT. TAhanx!!! thankyou all.

10 replies

by wlhs

in the second list the
"first last"
line should be under the "add1 add2" field.
thanks

by bob sellman

Here's at least an idea of how you might be able to proceed, if I understand what you want to do.

Let's assume you have created a new table TblNew with no data (just column definitions) from the current table CurrTable.

Open a recordset rst from CurrTable. I assume there is something that will cause the current table to sort in the correct order (even if it is an autonumber column).

Then, using rst.movelast and rst.movefirst, set an integer variable to the record count and begin to loop through each record in the recordset. (For intX = 0 to RecordCount - 1).

Test the values of the number columns you mention. If none of them has changed in value, then just insert a record into TblNew using the current values of rst (from the current record of rst).

Otherwise, before doing the insert above, insert an additional record into TblNew using the values you want from rst.

I realize that you'll have to figure out the detailed code, but perhaps this will point you in the right direction.

Good luck.

by bob sellman

Some added thoughts...
If you are using an autonumber column for the row order, then the new table TblNew will also automatically number the rows in the correct order, since that's the way you are adding them.

Do you really need a new table? Ifyou have a way of controlling the order of the rows by sorting on column values (other than an autonumber key), then you might be able to use a similar scheme just to add additional rows to the same table, or perhaps to a temporary table that after you have run through all rows in the recordset you would append to CurrTable.

Don't forget to move to the next row in rst within the for to next intX loop before you get to the next intX statement.

by bill_h

Based on our e-mail discussions, I think this will do what you want...the text is longer than will fit in the answer, so I'll send part 2 as a comment.

'-------- PART I --------
Private Sub Copy_File()
Dim cnAccess As ADODB.Connection Dim rsCopyHdr As ADODB.Recordset
Dim rsCopyFile As ADODB.Recordset
Dim sSQLString As String

'Create Connection
Set cnAccess = New ADODB.Connection
cnAccess.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password='';" & _
"Data Source=C:\MyDatabase.mdb;Persist Security Info=True"
cnAccess.CursorLocation = adUseClient
cnAccess.Open

'Use DISTINCT keyword to get values for spacer record
Set rsCopyHdr = cnAccess.Execute("SELECT DISTINCT Field1,Field2,Field5,Field6 " & _
"FROM CurrTable GROUP BY Field1,Field2,Field5,Field6")
If rsCopyHdr.RecordCount > 0 Then
'Get first spacer record
rsCopyHdr.MoveFirst While Not rsCopyHdr.EOF
'For each spacer record, get all detail records
Set rsCopyFile = cnAccess.Execute("SELECT * FROM CurrTable " & _
" WHERE Field5=" & rsCopyHdr.Fields(2).Value & _
" AND Field6=" & rsCopyHdr.Fields(3).Value)
'Get first detail record
rsCopyFile.MoveFirst
'--------End of PART I --------

by bill_h

-------- PART II --------
While Not rsCopyFile.EOF
'Insert detail record into new table
sSQLString = "INSERT INTO NewTable VALUES('" & _
rsCopyFile.Fields(0).Value & "','" & _
rsCopyFile.Fields(1).Value & "','" & _
rsCopyFile.Fields(2).Value & "','" & _
rsCopyFile.Fields(3).Value & "'," & _ rsCopyFile.Fields(4).Value & "," & _
rsCopyFile.Fields(5).Value & ")"
cnAccess.Execute sSQLString
'Get next detal record
rsCopyFile.MoveNext
Wend 'Insert spacer record into new table
sSQLString = "INSERT INTO NewTable (Field3,Field4) VALUES('" & _
rsCopyHdr.Fields(0).Value & "','" & _
rsCopyHdr.Fields(1).Value & "')"

cnAccess.Execute sSQLString
'Get next spacer record
rsCopyHdr.MoveNext
Wend
End If
Set rsCopyFile = Nothing
Set cnAccess = Nothing
End Sub
-------- End of PART II --------Hope this helps...let me know if you have any question.

This discussion is closed to new replies.