moving information two cells to the right

mfaqueiroz

Registered User.
Local time
Today, 01:23
Joined
Sep 30, 2015
Messages
125
Hello,

I have a historic table, and everytime that i introduce new data I need to move all the information two cells to the right. As the follow picture:

move2cells.png

Do you know how can i do that using vba?

thanks!
 
You don't.

What you're talking about is something you may do in spreadsheets, but despite data sheets LOOKING like spreadsheets, they're absolutely not.

Instead, why don't you tell us precisely what you're doing, what the fields and data mean, and WHY the values need to be 'moved two cells to the right'.

You can certainly run a series of commands changing the values in each of the 6 affected fields in each record, but it strikes me as a bad solution, especially as you have enumerated fields, which are typically a sign of bad database design. If your system is laid out poorly, it's going to make your life more and more difficult as you continue to develop or add on to the application.
 
Last edited:
Thanks Frothingslosh.
Well this is a historic with the last 3 dates that the machine was activated and the frequency.
T (most recent) (e.g. july)
T-1 (e.g. may)
T-2 (less recent) (e.g. fev)

I run every month the data of this month. So if my machine is activated on august, i should upload this table and my new values would be:
T august
T-1 july
T-2 may)
(i remove the fev, and i move the others )

Do you have any suggestion how can i do that?
 
What I would do is create a separate history table. Each would have, say, its own primary key, the key assigned to the machine, the date it was activated, and the frequency. You could throw in any other information related to each specific activation, as well - who, what, where, when, why, whatever.

Normally, you would just leave that information alone, and use a query to just pull up the three most recent occurrences. Doing it that way lets you look back through the entire history if you need to, while not overloading the user. (Personally, I'd just use a form/subform setup, so that someone could look at each machine and see its entire history.)

If you really want to limit the history to the most recent three activations, then you can create a delete query that will delete all but the three most recent activations, but that's going to take a couple steps. Basically, you make one query that pulls the top three results from the history table that match the specific machine, sorted by descending date. Then you make a delete query with a left join on the query you just made (ie - properties will be 'all records from history and only matching records from' the query), have it select all the records from the history that match the machine's ID but whose history ID values are NOT in that other query.

Clear as mud? :D

Edit: I've attached an example of what I meant by a form/subform setup to show the history. Sorry, can't access Imgur from work, or I'd host it and just embed it in the post.
 

Attachments

  • GroupSize.jpg
    GroupSize.jpg
    91.6 KB · Views: 124
Last edited:
Your other option if you don't want to redo your table setups is to create a basic VBA function that starts with the T2 field, copies the value from the matching T1 field, then does the same with T1 and T, then blanks T, then does the same with the F fields. It will certainly work for now, and will take less time up front, but you'll still be left with bad design causing some massive potential problems down the road.
 
Frothingslosh, i'm very gratefull for your help.
Only to understand, with vba code how can i pull the information?
i tried the follow code, logically didn't work: it writes the same information in all the rows....:/

Sub writeHistoric()

Set dbs = DBEngine(0)(0)
Set Historic = dbs.OpenRecordset("HistoricT", DB_OPEN_DYNASET)

Historic.MoveFirst
Do While Not Historic.EOF

Historic.Edit
Historic.Fields(5) = Historic.Fields(3)
Historic.Fields(6) = Historic.Fields(4)
Historic.Fields(7) = Historic.Fields(5)
Historic.Fields(8) = Historic.Fields(6)
Historic.Fields(9) = Historic.Fields(7)
Historic.Fields(10) = Historic.Fields(8)


Historic.Update

Historic.MoveNext
Loop

End Sub
 
I'm sorry!
just realized that i should simply do in the inverse way!
...
Historic.Edit
Historic.Fields(10) = Historic.Fields(8)
Historic.Fields(9) = Historic.Fields(7)
Historic.Fields(8) = Historic.Fields(6)
....
 
Yeah, that's the short-term fix. Won't solve the underlying problems (what do you do if your boss suddenly wants six months history? How do you compare stats over multiple activations?), but it'll get you running for now.

Keep in mind, from looking at your code, that it will a) change EVERY entry in your history table, not just a selected few, and b) be very, very slow to run. You might be better off creating an update query and have your VBA code execute it.
 
Just to reinforce what Frothy was saying you should simply store the event dates sequentially and produce reporting around that data. Unless you are dealing with millions of records there is no reason to remove / delete / archive data.
 

Users who are viewing this thread

Back
Top Bottom