Required text box with Save Button on form

inkbird1

Board Regular
Joined
Apr 21, 2020
Messages
51
Hi, I have a data entry form with a save button - I am wanting to check that the text box has a value before submitting to table.

VBA Code:
Private Sub btn_save_Click()

Dim strSQL        As String
  Dim DB            As DAO.Database
  Dim RS            As DAO.Recordset
  Dim ctl           As Control
 
  Set DB = CurrentDb()
  Set RS = DB.OpenRecordset("Tbl_users", dbOpenDynaset, dbAppendOnly)

  'Add user to table
    RS.AddNew
    RS!UserName = txt_username
    RS!FirstName = txt_firstname
    RS!FirstName = txt_lastname
    RS!Password = txt_genpassword
    RS!AccessLvl = cbo_AccessLvl
    RS!Facility = cbo_facility
    RS!Email = txt_email
    RS!IsActive = "Yes"
    RS!CreatedBy = Environ("USERNAME")
    RS!CreateDate = Now()
    RS.Update
   
        MsgBox "Record Successfully Added"
   

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Not sure why you'd open a recordset for this when you can just run a query or a sql statement or even just make it a required field in table design (assuming the control is bound to a field). Anyway, something like
VBA Code:
If Not Nz(Me.txtMyTextbox,0) = 0 Then
 Set DB = CurrentDb()
 Set RS = DB.OpenRecordset("Tbl_users", dbOpenDynaset, dbAppendOnly)
'rest of code

End If
If you SET a variable to something, at the end you should set it to Nothing.
 
Upvote 0
Not sure why you'd open a recordset for this when you can just run a query or a sql statement or even just make it a required field in table design (assuming the control is bound to a field). Anyway, something like
VBA Code:
If Not Nz(Me.txtMyTextbox,0) = 0 Then
 Set DB = CurrentDb()
 Set RS = DB.OpenRecordset("Tbl_users", dbOpenDynaset, dbAppendOnly)
'rest of code

End If
If you SET a variable to something, at the end you should set it to Nothing.
Thank you for your reply.
What would the correct way be to insert this record whilst also having the text field validation?
 
Upvote 0
The simple validation is what I posted. If you want something more robust, such as only allowing numbers, or text or whatever else, then that would be added to the IF block. As I mentioned, you'd either build a sql statement and run or execute it or run a query. Having said that, if there is something about that query that would make it not updatable, then yes you would use recordset(s). When executing a sql statement you don't have to turn off warnings, so it's my preferred method. Look up ms access CurrentDb.Execute if interested.

Now that I've re-read your first post I have to wonder why the form isn't just bound to a table or query and you do validation using form or control BeforeUpdate event? No executing, no running query, no code to build sql statements?
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top