what is the best way to search table and after insert selected data into current rec.

fafik1979

New member
Local time
Today, 21:50
Joined
Oct 14, 2008
Messages
7
hi
my problem here is to find a concept. i have table with fields:
name, duty,start_time, finish_time, new_duty, new_name, new_start_time

now what i need to do is access to find within the table value i would enter into new_duty field in all duty field records ( find new_duty in duty ) that start_time > now() and sort them in ASC order pick the first one as i need the start_time to be closest to now() and from that record copy name->new_name start_time->new_start_time
i know it's a bit unclear but cant find the way to explain it better. in other words i need access to find record with the person that will next (closest to now() in the future) takeover given duty number
i was thinking of creating an sql to find all duties i want to look for with time starting after now moving to the first record and than copying values into fields in my table.
if you think there is a better way please let me know i'd be grateful

fafik1979
 
thx
problem is that i have to have 2 mr. smiths records as he wrks 2 half's on different duty numbers, times etc and because duty numbers often mix due to some problems employees swap and i need to find out say if mr smith was on duty 11 but is now on duty 45 i need to find out who should be on it or time next employee takes over duty

fafik1979
 
hi again
purpose of this database is to help monitor employees time at work. because they are on "mobile" duties this database is to help cross match changing duties and help to optimize HR management. now this database shrinks as the day goes. the new_duty, new_start_time, new_name fields are there only to help identify possibility of nearest relief and nothing else when the employee gets back on his right duty these fields would be cleared as they then become obsolete. because duties are different everyday so it would be hard to do it other way. i would think ?

thanks for reply
fafik 1979
 
hi
i will post one but not before tuesday as i am very busy at work.

thx
fafik1979
 
hi again
so far so good. i just found a very nice solution on one of the forums, hope you guys don't mind if i post it in here (source:forums.devshed.com):
Code:
[URL="http://forums.devshed.com/member.php?u=32194"]Shad0w[/URL]                        [IMG]http://images.devshed.com/fds/statusicon/user_offline.gif[/IMG]                                                            
                                 Junior Member
                  [LEFT][IMG]http://images.devshed.com/fds/belts/ds_forums.gif[/IMG][/LEFT]
                                                                                                                      Join Date: Jul 2003
                                                                    Posts: 1                         [IMG]http://images.devshed.com/fds/reputation/reputation_green.gif[/IMG]   
Time spent in forums: < 1 sec          
Reputation Power: 0                     
                           
                        
                 
                                                                                                                                                                 [B]saving a list off[/B]             
                                                                        First post so i'll try and give an answer.
This post was actually written on another message board by me 1 day before. I asked a similar question but ended up solving it myself. This may slightly differ from what you want but you should be able to use parts of it.

I needed to make a hybrid sql/value list combination to add to a combo box of which the select result from the combo box would filter a subform table.

anyways here we go sorry if some of it doesn't seem to flow as below here was posted on another forum.

------------------------------------

For anyone that may need it here is the answer. While i won't back it up as being the most efficient answer as i'm sure there may be a better way in existance. I'm pretty new to vb and it bamboozles me with all the stuff you don't have to create thats there to use especially with access integration.

However i feel kinda lame answering my own question but here it is.

i'll assume you have a table name ContractDetails with field ContractID and that your
combo box is called cboContractNumber. I'll also assume that the form with the combo box on it is called "Form_frmYourForm" and your using the dao record set rather then the ado (active data object) record sets since i haven't learnt to use it yet but i'm told dao is old school and ado is better. Anyways on with the show.

Please note however that i haven't added any validation error checking but the only part that needs it is you MUST check to see if the table query returns any rows before calling the MoveLast command. If there are no rows returned then you can't really move to the last record of a null set i guess :/ and you'll get a crash and burn situation.

I actually placed this code in the open event of the form but it could be used anywhere.

CODE  

' variants can take string, integer whatever data we'll need one of these for
' the database field retrieval. I could use a string since i know the contractID is one
' but better to get into the habit of using variant for later more robust/reusable code
Dim strValueList As Variant
Dim strListQuery As String
Dim strFinalStringList As String

Dim wks As Workspace
Dim db As Database
Dim recContractDetails As DAO.Recordset

Dim iTotalrows As Integer
Dim iCounter As Integer

' set the query we are going to run
strListQuery = "SELECT [tblContractDetails].[ContractID] FROM tblContractDetails;"

Set wks = DBEngine.Workspaces(0)
Set db = wks.Databases(0)

Set recContractDetails = db.OpenRecordset(strListQuery, dbOpenSnapshot)
recContractDetails.MoveLast

iTotalrows = recContractDetails.RecordCount

' grab all the rows. Don't worry the variant will be automatically resized to a
' multi dimensional array
strValueList = db.OpenRecordset(strListQuery, dbOpenSnapshot).GetRows(iTotalrows)

iCounter = 0

' our first value of the field list is set to "All"
finalstring = "All;"

Do While iCounter < iTotalrows    ' Inner loop.

   ' append to it all the extra values
   finalstring = finalstring + strValueList(0, iCounter) + ";"
   iCounter = iCounter + 1    ' Increment Counter.
   
Loop

Form_frmYourForm.cboContractNumber.RowSource = finalstring
 


Also note that you must add some extra code in the cboContractNumber onChange event. You'll have to go something like

CODE  

if(chosen item from box == "All")
{
       //then filter the table you have displayed with this sql command
       docmd.runsql(select * from whatever_table)
}
// its one of the list items from the table so use the normal sql query
else
{
      docmd.runsql(select * from whatever_table where contractID = cboContractNumber.value
}
 


The last bit was semi pseudo/vb/c++ code so if you've gotten the above to work you should be able to fix that up to work.

Anyways it took me some microsoft assistant help searching time and lots of bits and pieces on scrap paper to finally get it to work the proper way.

Anyone with a better/cleaner/more efficient solution please don't hesitate to post it. Else enjoy the code... maybe it can help someone else.

thanks for your time

------------------------------------

If you wish to see the full posting then you can at the following link

Click Here For The Link


I'm not advertising or anything so please don't get mad i merly thought the link may direct others that want to read the full thing to get the idea of what i did and was trying to do.

But again i don't think its the most efficient way so if people can tidy the code up make ti smaller and better then by all means [IMG]http://images.devshed.com/fds/smilies/smile.gif[/IMG] as i'm not the best vb coder out there considering i've used it for around 2-4 weeks.

Anyways i hope it helps.
                                                                                                              [I]                 Last edited by Shad0w : July 16th, 2003 at 05:40 AM.
[/I]
looks like something i was looking for. can get sql results into workspace and then separate wanted data of it. done some preliminary tests and i think i like it. will post back to let you know.

thx
fafik1979
 

Users who are viewing this thread

Back
Top Bottom