View Full Version : Copy data from one column to another bases on a specific id


haavar
07-01-2007, 11:01 PM
Hi!

I want to find and copy data from one column to another based on a name.

For example:


'Go down the column,every record...
while (tagname != "")
{
' If found the same, copying data
if(tagname == tag)
Copy data from the columns to a new table
}


Do someone have a suggestion?

Dennisk
07-01-2007, 11:19 PM
I would use an update query myself. Open the QBE and first create a select query using the name as the criteria. then turn it into an update query and specify what column is updated and to what vaue.

haavar
07-01-2007, 11:25 PM
I see what you mean, but I have 2000 records with different tags I must identfy with code and then find data who is linked together in another table. The link is between the unique tag.

I would use an update query myself. Open the QBE and first create a select query using the name as the criteria. then turn it into an update query and specify what column is updated and to what vaue.

haavar
07-02-2007, 12:56 AM
Hi! Thanks for the code, but I have a problem with If rec!Name = "ELL" Then
thats because I need find the Name auto from table. The variable changes all the time. And how do I link it to another table. feks. tablename.field


If its going to be a lot of records that you have to update.
Best write a sub.
Here is an example of a simple recordset sub


Option Compare Database
Option Explicit

Sub sMoveValue()
'Lister 2007
'Make sure you have set your Tools > References > Microsoft DOA 3.6 Object Library
On Error GoTo ErrHandler

Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim strSQL As String
Dim inCount As Integer

strSQL = "Select * from tblOpenBalance" 'This string can be a query name or pure SQL

Set db = CurrentDb() 'Sets the database as the current database.
Set rec = db.OpenRecordset(strSQL) 'Opens the recordset
inCount = rec.RecordCount 'Counts to see if there are records
If inCount > 0 Then 'If there are time to go to work
rec.MoveLast
rec.MoveFirst 'Make sure we know how many records we are going to be working with
Do Until rec.EOF 'Start the loop, do until we run out of records.
If rec!Name = "ELL" Then 'If rec![Name of your field] = criteria Then. You could set as a variable
rec.Edit 'We are going to Edit the record.
rec!strNewDocType = rec!DocType 'We are going to copy the data from rec![This field] inot rec![That field]
rec.Update 'Having done that update
End If
rec.MoveNext 'Move to the next record
Loop 'And Loop
End If
ExSub: 'Now we turn it all off.
rec.Close
Set rec = Nothing
Set db = Nothing
Exit Sub
ErrHandler:
MsgBox "Error Number: " & Err.Number & " - " & Err.Description, vbInformation, "Opps: ERROR!"
GoTo ExSub

End Sub


Benifit is that you can call the sub when ever you need it.
And you could make it much more dynamic if you wanted.

Hope that helps. :)

haavar
07-02-2007, 01:28 AM
Hi! Thanks for the code, but I have a problem with If rec!Name = "ELL" Then
thats because I need to find the Name auto from table. The variable changes all the time. And how do I link it to another table. feks. tablename.field