473,320 Members | 1,828 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,320 software developers and data experts.

How to make a form not save to a table if left blank?

4
I know this question had been asked before but i never got a solution.

I have form bounded to a table, i would Not like this form to save any blanks. If i type something on text box than change my mind and erase. When i close the form it should not add any blank records in an underlying table.

How can this be achieved through VBA or Macro..any way? Please give me an example code or Macro.

My form is frm123. My table is tbl123 with fields one, two, three.

More info: I am using MS access 2003.

Please assist.

Kim
Feb 7 '11 #1

✓ answered by TheSmileyCoder

In most of my forms I have code like such:
Expand|Select|Wrap|Line Numbers
  1. Private sub Form_BeforeUpdate(Cancel as integer)
  2.   Cancel=Not fBoolValidateForm()
  3. End Sub
  4.  
  5. Private Function fBoolValidateForm() as Boolean
  6.   Dim strErrMsg as string
  7.   If Me.tx_UserName & ""="" then
  8.     strErrMsg=strErrMsg & "User Name not filled out" & vbnewline
  9.   End If
  10.  
  11.   If Me.tx_UserInitials & ""="" then
  12.     strErrMsg=strErrMsg & "User Initials not filled out" & vbnewline
  13.   End If
  14.  
  15.   If Me.tx_UserBirthDay & ""="" then
  16.     strErrMsg=strErrMsg & "Birthday Not filled out" & vbnewline
  17.   else
  18.     If not isDate(Me.UserBirthDay) then
  19.         strErrMsg=strErrMsg & "Birthday Date is not in a valid format" & vbnewline
  20.     End If
  21.   End If
  22.  
  23.   if strErrMsg & ""="" then
  24.     'No errors found
  25.     fBoolValidateForm=True
  26.   Else
  27.     'Some errors found
  28.     fBoolValidateForm=False
  29.     strErrMsg="Could not Save record due to missing or invalid information." & vbnewline & _
  30.               "Please correct the following before saving:" & vbnewline & vbnewline & _
  31.               strErrMsg & vbnewline & _
  32.               "Or press cancel now to exit without saving"
  33.     if vbcancel=msgbox( strErrMsg,vbokcancel+vbinformation,"Missing or invalid information) Then
  34.       'User wants to cancel
  35.       Me.Undo
  36.     End if
  37.  
  38.   End If
  39.  
  40. end Function
I hope that can help you.

6 1193
munkee
374 256MB
You want to use validation placed in to your before update event of the form.

In the validation you will want to check to make sure that the textboxes are not null. If they are null then you cancel the before update event.

Such as

Expand|Select|Wrap|Line Numbers
  1. If isnull(me.txtbox1) = true then
  2. cancel = true
  3. else
  4. End if
Feb 7 '11 #2
TheSmileyCoder
2,322 Expert Mod 2GB
In most of my forms I have code like such:
Expand|Select|Wrap|Line Numbers
  1. Private sub Form_BeforeUpdate(Cancel as integer)
  2.   Cancel=Not fBoolValidateForm()
  3. End Sub
  4.  
  5. Private Function fBoolValidateForm() as Boolean
  6.   Dim strErrMsg as string
  7.   If Me.tx_UserName & ""="" then
  8.     strErrMsg=strErrMsg & "User Name not filled out" & vbnewline
  9.   End If
  10.  
  11.   If Me.tx_UserInitials & ""="" then
  12.     strErrMsg=strErrMsg & "User Initials not filled out" & vbnewline
  13.   End If
  14.  
  15.   If Me.tx_UserBirthDay & ""="" then
  16.     strErrMsg=strErrMsg & "Birthday Not filled out" & vbnewline
  17.   else
  18.     If not isDate(Me.UserBirthDay) then
  19.         strErrMsg=strErrMsg & "Birthday Date is not in a valid format" & vbnewline
  20.     End If
  21.   End If
  22.  
  23.   if strErrMsg & ""="" then
  24.     'No errors found
  25.     fBoolValidateForm=True
  26.   Else
  27.     'Some errors found
  28.     fBoolValidateForm=False
  29.     strErrMsg="Could not Save record due to missing or invalid information." & vbnewline & _
  30.               "Please correct the following before saving:" & vbnewline & vbnewline & _
  31.               strErrMsg & vbnewline & _
  32.               "Or press cancel now to exit without saving"
  33.     if vbcancel=msgbox( strErrMsg,vbokcancel+vbinformation,"Missing or invalid information) Then
  34.       'User wants to cancel
  35.       Me.Undo
  36.     End if
  37.  
  38.   End If
  39.  
  40. end Function
I hope that can help you.
Feb 7 '11 #3
NeoPa
32,556 Expert Mod 16PB
In Smiley's code, you simply put whatever checks you want for your specific form into the function fBoolValidateForm().

Do you understand how to do that for your form?
Feb 7 '11 #4
Lysander
344 Expert 100+
Below is a generic function I use on all our forms, held in a global module

Basicly, every control on the form must either be filled in, or locked (not needed) or invisible (normally control fields)

The function returns a string of all the controls not filled in, and true/false.

If true, save the record, if false, display the list of controls not filled in and do not save the record.


Expand|Select|Wrap|Line Numbers
  1. Public Function Form_Check_NULL(ByVal frm As Form, ByRef strError As String) As Boolean
  2. On Error GoTo Form_Check_NULL_Err
  3. Dim ctl As Control
  4. Dim Form_Valid As Boolean
  5. Form_Valid = True
  6. strError = ""
  7. For Each ctl In frm.Controls
  8.     Select Case ctl.ControlType
  9.     Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox
  10.         If IsNull(ctl.Value) And ctl.Enabled = True And ctl.Visible = True And ctl.Locked = False Then
  11.             strError = strError & ctl.Name & "/"
  12.             Form_Valid = False
  13.         End If
  14.     End Select
  15. Next ctl
  16. Form_Check_NULL = Form_Valid
  17. Form_Check_NULL_Exit:
  18.    Exit Function
  19. Form_Check_NULL_Err:
  20.    MsgBox Err.Description & " in Form_Check_NULL"
  21.    Resume Form_Check_NULL_Exit
  22. End Function
  23.  
it is used on the form via a command button 'cmdRecordSave' as follows

Expand|Select|Wrap|Line Numbers
  1. If Check_Null = False Then
  2.     Form_Valid = False
  3.     showerror strError
  4. Else
  5.     DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  6.     MsgBox strTranslate("TR000000130", "Record Saved")
  7. End If
  8.  
  9.  
Feb 7 '11 #5
Chege
4
Thank you all. You are great people now I can move forward with my project.

kim
Feb 8 '11 #6
TheSmileyCoder
2,322 Expert Mod 2GB
Thats good to hear. Your welcome.
Feb 8 '11 #7

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

Similar topics

2
by: Bill Hand | last post by:
Is it possible to open a form from table and have it access a given record? I want to be able to query all baseball cards with the last name Jones and have it open a table. In each record of...
1
by: Jason Wood | last post by:
Hi everyone, I've a simple table containing member details. I've written a SELECT to select all the members. I've based the form on this select, using the from wizard. Once built I've...
1
by: David Boone | last post by:
Please excuse the newbie questions, but I'm fairly new to Access though familiar with databases in general (experience with MySQL and PostgreSQL). The basic idea is that there is table of...
3
by: KayC | last post by:
Hi Background info: I am running Access 2000 I have a form bound to a blank table When a user clicks a button the form opens in datasheet view User enters data into form and closes form User...
0
by: abhishekjethwani | last post by:
How to write a query to access tree structure form multi table. I m having five tables from them first table give me a data which act as the parameter for query for the second table and the two...
4
by: rawky1976 | last post by:
Hi, I have a hyperlink which goes to the page below and passes the PK_ID from a query into 'userid'. From this we query the DB and then echo a table with a form; only the textboxes are already...
5
by: slenish | last post by:
What im trying to do is cut down on saving blank records. At this moment when I hit save record it just saves a whole blank record. What I want to do is make it so when you hit save if certain...
15
by: hedges98 | last post by:
I am struggling with something that is probably quite simple. When I close a form, if certain fields are left empty then I want a message to pop up to tell the user these fields are required and then...
12
by: slenish | last post by:
Well i have been playing with this off and on all day and its driving me up a wall. I have a small form that has 5 text boxes. Now if I were to type nothing in to the pop up box before closeing...
5
by: Ron Mowry | last post by:
im trying to figure out how to create a adhock ticketing system through ms access. I have my main table with 20,000 records. Some records may not need to be modified some may need to be and others...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.