Access 2007 Form which 'autofills' (1 Viewer)

Toadums

Registered User.
Local time
Yesterday, 16:55
Joined
Feb 22, 2009
Messages
53
So..I have this form and it has about 6 fields, and what I want, is that when someone enters a project number (they can choose between already listen ones, cant make their own) then 3 of the other fields are automatically filled in based on the project number...I know there may be an easier way, and if you know it feel free to say :p

But all I know how to do is something like this...but it doesnt work. I get a "not found in collection" error...like if i enter a 1...and there is DEFINITELY a 1 in the collection...just not sure where I am going wrong.

Here is the code:

Code:
Private Sub Project_Number_AfterUpdate()

Dim rst As DAO.Recordset
Dim f As Integer

Set rst = CurrentDb.TableDefs!d.OpenRecordset

f = Me.Project_Number 'stores the value which is inputed into the form's textbox 


rst.MoveFirst
Do Until rst.EOF

If (rst!Project_Number = f) Then '<< error here
    
    With Me 'setting the other fields
    .Client = rst!Client
    .Project_Name = rst!Client_Name
    .Phase = rst!Phase
    
    End With
    
    End If
    
    rst.MoveNext
    Loop

End Sub

Thanks!
Paul
 

RuralGuy

AWF VIP
Local time
Yesterday, 17:55
Joined
Jul 2, 2005
Messages
13,826
Have you tried creating a form bound to a query of the table and use the wizard to add a ComboBox and pick option #3 from the wizard?
 

Toadums

Registered User.
Local time
Yesterday, 16:55
Joined
Feb 22, 2009
Messages
53
Cool, I will play around with combo boxes :D not sure exactly what you mean by a form bound to a query tho :p

Thanks
 

RuralGuy

AWF VIP
Local time
Yesterday, 17:55
Joined
Jul 2, 2005
Messages
13,826
A query looks just like a table to a form but it is far more powerful. You can do calculations and joins in a query. Just use the query builder to create a select query of your table and select all of the fields. Then use the Form wizard to create a form of the query. Then add a ComboBox with the cbo wizard. That will get you started.
 

Toadums

Registered User.
Local time
Yesterday, 16:55
Joined
Feb 22, 2009
Messages
53
Ok, I think I did what you said to do...lol...Im kinda new to Access, but I think I got it...then I assigned the combo box the following code using DLookup:

Code:
Private Sub Project_Number_AfterUpdate()

If (Project_Number <> Empty) Then
Client = DLookup("[Client]", "d", "[Project_Number]=" & Me.Project_Number)
Project_Name = DLookup("[ProjectName]", "d", "[Project_Number]=" & Me.Project_Number)
Phase = DLookup("[Phase]", "d", "[Project_Number]=" & Me.Project_Number)
End If

End Sub

it...sort of works...except regardless of what item I choose from the combobox, it always brings up the same values for project name, client, phase (the ones from the first record in the query d)

...not sure if im using dlookup wrong, or if i am even supposed to >.<
 

RuralGuy

AWF VIP
Local time
Yesterday, 17:55
Joined
Jul 2, 2005
Messages
13,826
Your ComboBox should *not* be using a DLookup() function. Did you create a new ComboBox control on your form and select option #2 "Find a record..." when asked?
 

Toadums

Registered User.
Local time
Yesterday, 16:55
Joined
Feb 22, 2009
Messages
53
Yeah, thats the way I did it the first time, but when I do that it..well doesnt change any of the other fields...

I am in a tabular view for my form (thats the way I need it to be) so im not sure if that affects it, since the one on the tutorial is a columnar view (i think.. lol) ...

Also, in the combo box when I select one of the values, it just changes the values in ALL the combo boxes in the column to the same thing...where am i goin wrong here >.>

and sorry for being such an access noob...the course i took wasnt terribly useful...lol
 
Last edited:

wiklendt

i recommend chocolate
Local time
Today, 09:55
Joined
Mar 10, 2008
Messages
1,746
Also, in the combo box when I select one of the values, it just changes the values in ALL the combo boxes in the column to the same thing...where am i goin wrong here

what is the source data for each of the controls?

can you post your db?
 

Toadums

Registered User.
Local time
Yesterday, 16:55
Joined
Feb 22, 2009
Messages
53
Ok, here is my db, please ignore the fact that it is prolly really bad, i have deleted a lot of stuff and added stuff i prolly dont need :)


Thanks,Paul
 

Attachments

  • TimeSheet.zip
    103 KB · Views: 145

wiklendt

i recommend chocolate
Local time
Today, 09:55
Joined
Mar 10, 2008
Messages
1,746
like if i enter a 1...and there is DEFINITELY a 1 in the collection...

i don't see a 1 in the collection (of project numbers). there's a 10, but it's not included in the query from which the form draws its data.

i'm not sure what you wanted to achieve with this form as it is already displaying all the records.

do you mean that you want to filter the records to display only those that match a certain criteria?

edit: instead of saying you have six fields and you want to fill in three...etc, i think you should tell us a bit of background on your project and what you are trying to achieve. like, "i'm tracking repairs my company contracts out. i want to display in a form which companies are assigned which jobs...." or whatever it is you are actually doing... that might help us a little more in figuring out how yoru form ought to be designed.
 
Last edited:

Toadums

Registered User.
Local time
Yesterday, 16:55
Joined
Feb 22, 2009
Messages
53
yeah, about the 1 :p i said that back in an old version of the DB hehe!

Ok, so pretty much what I want is this: When someone choses the projuct number, it will automatically fill the Client, Project name, Phase (this is a time sheet, and the people who I work with are Lazy, lol)

so say like Project number 355 is for the ministry of health, project name: security camera install, phase 2.

and the project numbers should be from a drop down box, people cant make their own :D

I really hope this makes sense >.<

Thanks for your patience, Paul
 

wiklendt

i recommend chocolate
Local time
Today, 09:55
Joined
Mar 10, 2008
Messages
1,746
yeah, about the 1 :p i said that back in an old version of the DB hehe!

Ok, so pretty much what I want is this: When someone choses the projuct number, it will automatically fill the Client, Project name, Phase (this is a time sheet, and the people who I work with are Lazy, lol)

so say like Project number 355 is for the ministry of health, project name: security camera install, phase 2.

and the project numbers should be from a drop down box, people cant make their own :D

I really hope this makes sense >.<

Thanks for your patience, Paul

ok, if i understand you, you will need to make some drastic changes to your database.

i think you've misunderstood the concept of "normalisation". search the forums here and google. basically, each separate 'thing' should have its own table with sensible names. e.g., tblEmployee, tblClients, tblProjects, tblPhases, tblTypes, tblItems... the list goes on but depends on what data you actually want to store. (names like "q" would not, i don't think, EVER be intuitive for a table).

typically, tables are prefixed with "tbl", queries with "qry", reports with "rpt", forms with "frm", modules with "bas"... and the list goes on (this is called a "naming convention" - if you search this forum or other website you can find a lot on it, e.g., queries are generally "qry", but a parameter query might be "pqry", and a crostab query might have "xqry")

then, you need to make sensible field names. calling every primary key (PK) in your database as "ID" will confuse you AND access in the future, depending on how many tables you end up having. i tend to do things following the table name, so tblEmployees would have the fields "EmployeeID"; "EmployeeName"; etc (never use "Name" or "Date" on their own, as they are 'reserved' access words), and tblClients would have "ClientID"; "ClientName"; "ClientAddress"; "ClientPhone"... etc. and so on.

Then you need to know how to connect the tables to 'link' or 'relate' all the data. for example, a project may have many phases, a client may have many projects.

so how do you link these? well, it's largely up to how your data flow goes, but most things are self-evident.

as a basic example: a project requires a client, as well as other project details. now, you already have a client table, so you don't need to put all the client data into the project data all over again, you just link the tables together via what is called a "foreign key" (FK)- a foreign key is basically the primary ID of the client table in the project table. it is sensible to call the client foreign key identically to the primary key, as it avoids your confusion on the future.

Now, your primary keys ought to be "autonumber" type, so that when you add a new record (a project, say) access will automatically create a number for you which is bigger than the last number you used (it never inherently reuses that numbner, even if you delete it) but your foreign keys only have to store that number, not generate it, so it is only a "number" type

for example, the project table could look something like this:

tblProjects
------------
ProjectID (PK, Autonumber)
ProjectName (Text)
Description (Text/Memo)
ClientID (FK, Number)
InvoiceSent (Y/N)
.
.
.

i tend to make the ClientID an easy combobox on my form. i was once told, and i regurgitated just to clarify, how to properly do this in this thread here. (start with post #18 on page two, and read ALL the way to the end of the thread).

then: back to what you said you wanted - to select a project number and have some details automatically filled. there are two scenarios (for example):

1) you have a list of clients and want to update a particular detail/field - so you want to select the client from a drop-down box (combobox) or a listbox and have their details displayed for you so you can change any of them.

2) you have an existing project, and you want to select one from the combo/list and add a new phase/client/item/job to it (or whatever you add to your projects). in this case, you would have a similar setup to (1), but you would also have a 'subform' of the data which is related to the project - which one specificly will depend on your own database and requirements.
now. there are some other basic things i would urge you to do, which aren't directly related to setting up tables, queries etc:

A) always keep backups of your database. there are hundreds of threads on this forum alone which are dedicated to various methods of backing up - from physically copying the file in windows explorer, to making access do it at the press of a button, to automating at a specific time (e.g., weekly, monthly, other) etc etc...

B) i personally, though i don't know about anyone else, choose to setup my databases to "compact on close". in access 2007, you get there by selecting the office orb, then "access options" right down the bottom, then "current database", then check the "compact on close" option. what this does i'm not exactly sure, but the most obvious effect is that it can drastically reduce your database file size - to do this only sporadically, you can go to the office orb, then "manage" then "compact and repair".
i did point (B) on your database and it went from almost 4 Mb to just under 400 kb.

i think your misunderstanding of database design is why you are having difficulty explaining what you want, and why the rest of us are having difficulty understanding your wants. hang in there! i'm pretty sure my first database was no better than yours - just have a think about what i've said here, and read the link i've provided, then have a good attempt at starting again, then come back if you get stuck - we'll all still be here to help :)
 

Toadums

Registered User.
Local time
Yesterday, 16:55
Joined
Feb 22, 2009
Messages
53
Wow, thanks so much for all your help!! I'm currently working on normalizing my database :D

Like I said..Im new to access...I have been databasing for like 2 days now hehe (the course i took a month ago was useless! we spent like 20 minutes the first day discussing selecting multiple items with shift >.>) I have been in access for a couple weeks, but before this I was just programming...lol which is easier (for me) and more fun!

Yes, I think there was definitely some ambiguity relating to what it was that I wanted to achieve with this database...just a simple time sheet to send out to my coworkers that they will in every 2 weeks...and then eventually, try to make it web based (i have MINIMAL knowledge of that), but for now my uncle (employer) said just get something done in access....He also wanted me to look into front/back end databases for it...which kind of intrigue me :D

Thanks again, and im sure i will have another question or two along the way :)
Paul
 

wiklendt

i recommend chocolate
Local time
Today, 09:55
Joined
Mar 10, 2008
Messages
1,746
we all started like that, i'm sure ;)
 

wiklendt

i recommend chocolate
Local time
Today, 09:55
Joined
Mar 10, 2008
Messages
1,746
i'm currently talking someone else through some of these same aspects in another thread, it might take your understanding a bit further :)
 

Toadums

Registered User.
Local time
Yesterday, 16:55
Joined
Feb 22, 2009
Messages
53
here's a post i spent 1.5hrs writing yesterday on almost this exact topic

Wow!! 1.5 hours. thats amazing, thanks!! haha. :D << yeah thats a smiley face...cause i got it working!

I realized that your PK doesnt have to be an autonumber..heh..who woulda guessed?!

Here Is my database if you want to have a look :)

Thanks again,
Paul
 

Attachments

  • timesheet2.zip
    21.5 KB · Views: 133

wiklendt

i recommend chocolate
Local time
Today, 09:55
Joined
Mar 10, 2008
Messages
1,746
Wow!! 1.5 hours. thats amazing, thanks!! haha. :D << yeah thats a smiley face...cause i got it working!

I realized that your PK doesnt have to be an autonumber..heh..who woulda guessed?!

Here Is my database if you want to have a look :)

Thanks again,
Paul

primary keys are best as autonumbers, it's FOREIGN keys that can't be autonumbers...
 

wiklendt

i recommend chocolate
Local time
Today, 09:55
Joined
Mar 10, 2008
Messages
1,746
but your db is much better :) glad you got it working.

good luck with the rest of it :)
 

Users who are viewing this thread

Top Bottom