473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I AutoFill fields if a Primary Key value has already been taken?

For the database I'm building, users will have to fill data into a form that uses a date as the Primary Key for the related table. The scenario for which I need the form to auto-fill is as follows:

If the user wants to enter data for 1/31/2012, but he or she forgot that data for that day had already been entered, is there a way that once they begin filling out the form and type "1/31/2012" in the Date (i.e. Primary Key) field, the rest of the fields will auto-populate with the previously entered data or the form will actually go to that exact record? Either would work, but I suppose it would need to actually go to the record as a duplicate Primary Key would be created?

Thanks!

JC
Feb 16 '12 #1

✓ answered by ADezii

This is a little trickier than you think, and would require some Logic in the AfterUpdate() Event of the DOB Text Box similar to:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDOB_AfterUpdate()
  2. Dim dteDate As Date
  3. Dim txt As TextBox
  4.  
  5. Set txt = Me![txtDOB]
  6. If IsNull(txt) Or Not IsDate(txt) Then Exit Sub
  7.  
  8. dteDate = txt
  9.  
  10. 'If we get here, we have a Valid Date, but does a Record exist with this Date
  11. If DCount("*", "tblEmployees", "[DOB] = #" & txt & "#") Then
  12.   MsgBox "A Record already exists with a Date of [" & txt & "]!", _
  13.           vbExclamation, "Date Duplication"
  14.     DoCmd.RunCommand acCmdUndo          'UNDO any changes made?
  15. Else       'Do nothing if a Record does not exist for this Date
  16.   Exit Sub
  17. End If
  18.  
  19. 'Search for the Date entered into txtDOB in ALL Fields
  20. DoCmd.FindRecord dteDate, acEntire, False, acSearchAll, False, acAll
  21. End Sub
P.S. - I realize that DOB (Date of Birth) is probably a bad example, but it is for Demo purposes only.

3 2916
NeoPa
32,556 Expert Mod 16PB
I think you'd need to code that yourself carefully, as there are a few gotchas that make it somewhat less straightforward than your brief explanation allows for. You'd need the code in the AfterUpdate event procedure of the PK control, but you'd need to handle (at least) saving any other data already entered into the form prior to the PK date being entered.
Feb 16 '12 #2
ADezii
8,834 Expert 8TB
This is a little trickier than you think, and would require some Logic in the AfterUpdate() Event of the DOB Text Box similar to:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDOB_AfterUpdate()
  2. Dim dteDate As Date
  3. Dim txt As TextBox
  4.  
  5. Set txt = Me![txtDOB]
  6. If IsNull(txt) Or Not IsDate(txt) Then Exit Sub
  7.  
  8. dteDate = txt
  9.  
  10. 'If we get here, we have a Valid Date, but does a Record exist with this Date
  11. If DCount("*", "tblEmployees", "[DOB] = #" & txt & "#") Then
  12.   MsgBox "A Record already exists with a Date of [" & txt & "]!", _
  13.           vbExclamation, "Date Duplication"
  14.     DoCmd.RunCommand acCmdUndo          'UNDO any changes made?
  15. Else       'Do nothing if a Record does not exist for this Date
  16.   Exit Sub
  17. End If
  18.  
  19. 'Search for the Date entered into txtDOB in ALL Fields
  20. DoCmd.FindRecord dteDate, acEntire, False, acSearchAll, False, acAll
  21. End Sub
P.S. - I realize that DOB (Date of Birth) is probably a bad example, but it is for Demo purposes only.
Feb 17 '12 #3
Perfect! Thanks ADezii! After a few modifications, I got the code to work. :)
Feb 17 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: shortbackandsides.no | last post by:
I'm having a lot of difficulty trying to persuade the Google toolbar autofill to act consistently, for example ======================= <html><head> <title>autofill test</title> </head><body>...
20
by: Romeo Colacitti | last post by:
Hello C-goers, Been using last C99 draft for a while now and want to purchase actual C99 standard from ANSI. I know there are two technical corrigendum documents standardized too, which are...
0
by: Gian Paolo Clarici | last post by:
I'm migrating one Asp page to Aspx. There is a recordset and I need to Reponse.write the content of one of its fields . That field comes from a Numeric(5,2) SQL datatype Its content is : 90.00 ...
5
by: Phil Latio | last post by:
I have 2 virtually identical tables and wish to move data between them. Basically one table is called "live_table" and the other is named "suspended_table" and the only difference is that the...
3
by: Emily Jones | last post by:
This: TRANSFORM First(.Text) AS FirstOfText SELECT .Expr1000 FROM _allText GROUP BY .Expr1000 PIVOT .LetterSectionID; hangs the application if _allText.Text is a memo field. But it's fine...
3
karthickRAJKUMAR
by: karthickRAJKUMAR | last post by:
what is my problem when i try to insert a value through php,the value is already exit in database(mysql primarykey) what is the error msg i get, or how can give error msg to the end user
12
by: Modern Merlin | last post by:
Ok please forgive me, as I am really new to the whole PHP programming... What I am attempting to do is on a form page create a drop down list that is populated from information in a database. For...
9
by: ramdil | last post by:
Hi All I have a list box in a form with three columns 1.ID 2.Name 3.Age In the same form i have three text boxes corresponding for the above columns. The form record source property is set to...
2
by: Appu2008 | last post by:
Hi, I have a dictionary whose keys are variables. Eg: - dict=None dict=value Here lst and values are 2 variables. How can I make the key as the value of lst. For eg:- if lst="node1"...
5
by: dmorand | last post by:
I'm having a strange issue with a form I'm developing. I'm saving some values to hidden fields when a user clicks a button. I setup a function which gets ran on submission of the form, but I also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.