i am new to ms-access, and i have started to look at code that works. unfortunately it is completely uncommented.
Private Sub OKbut_Click() Dim dt As Date dt = Now() Set rstOrder = New ADODB.Recordset rstOrder.Open "tblUsers", CurrentProject.Connection, adOpenStatic, adLockOptimistic If rstOrder.Supports(adAddNew) Then With rstOrder .AddNew .Fields("title") = title .Fields("first") = first .Fields("last") = last .Fields("gender") = gender .Fields("date_submitted") = dt .Update End With End If rstOrder.Close Set rstOrder = Nothing Dim rs As DAO.Recordset Set rs = CurrentDb.OpenRecordset("SELECT id FROM tblUsers WHERE date_submitted=#" & dt & "#") duser = rs.Fields("id") rs.Close Set rs = Nothing Do While Not user_defined(duser) DoCmd.OpenForm "define_user_frm", , , , , acDialog Loop 'Forms(0).user_lst.RowSource = "select * from users where id=" & duser Me.SetFocus DoCmd.Close End Sub
- what does the function Now() do?
- ADODB.Recordset is just a way to connect to a table?
- adOpenStatic, adLockOptimistic what are these?
- why are we checking this: if rstOrder.Supports(adAddNew) ?
- why do we need to do this: Set rstOrder = Nothing?
- what does this do:? Do While Not user_defined(duser)
DoCmd.OpenForm "define_user_frm", , , , , acDialog
Now() returns the current system date and time
Yes - more importantly, it is a good way to iterate through records of a dataset individually. Like a cursor in SQL Server. You could do something like
adOpenForwardOnly - this is the default cursor if no other is specified. This cursor allows only forward movement through a recordset
adOpenKeyset - this cursor supports forwards as well as backwards navigation. It also allows you to update a recordset and all changes will be reflected in other users recordsets. The cursor also supports bookmarking
adOpenDynamic - this cursor supports forward and backward navigation but bookmarks may not be supported (ie Access). Any changes made to data are immediately visible with no need to resynchronise the cursor with the database
adOpenStatic - this cursor uses a static copy of data from the database and therefore no changes to the data are visible and supports forward and backward navigation
It's a way of writing more robust code - i.e. before attempting to add a new record, first check whether the recordset supports the addition of new records.
Frees the recordset from memory. Not absolutely necessary but again, makes for more robust code.
Checks for existence of a user and if it doesn't exist, it opens a form called "define_user_frm" which I assume allows for creating a new user.