![]() You probably don't need to set these variables to Nothing - VB does it for you, anyway. And INSERT statement returns no values: Set rstRecordSet = cmdCommand.Execute It is the order in the INSERT statement that matters.Prepared = Trueĭon't use a return value in use the following line. ![]() Note that the actual parameter names, e.g. Replace them with the correct field names.CommandText = "INSERT INTO laptopLoggedInLoggedOutInfo(f1,f2,f3,f4,f5,f6,f7) VALUES (?,?,?,?,?,?,?) " RstRecordSet.Open "SELECT MAX(logInId) FROM laptopLoggedInLoggedOutInfo", conConnection logInId = IIf(IsNull(rstRecordSet.Fields(0)), 0, IsNull(rstRecordSet.Fields(0))) With cmdCommandĪre these really your fieldnames: f1, f2, f2, etc.? I would be surprised. My suggestion: use "SELECT MAX(f1) FROM laptopLoggedInLoggedOutInfo", and retrieve the first value, ensuring that if the value is null, it returns zero, e.g. rstRecordSet.Open "laptopLoggedInLoggedOutInfo", conConnection Generally, if you want a unique ID, you must ensure that the ID always increments. If you delete records from the table, then your ID will become smaller. I see that you are using a recordset just to receive a count of records in logInId. conConnection.ConnectionString = "Provider=.12.0 Data Source=" & _ Make sure that the Data Source substring is points to your Access database. Dim conConnection As New ADODB.Connection This may not necessarily be what you intend. Essentially, "As New" declared variables are checked every time you try to use a property or method on them, and if the variable Is Nothing, it creates a new instance. ![]() It is better if you instantiate them manually. Whilst this doesn't matter for this particular example, generally don't declare object variables "As New". I've made in-line comments on your code: Private Sub Command1_Click() CreateParameter("f7", adDate, adParamInput,, logInTime) CreateParameter("f6", adDate, adParamInput,, logInDate) CreateParameter("f5", adChar, adParamInput, 20, laptopBrand) CreateParameter("f4", adChar, adParamInput, 20, laptopName) CreateParameter("f3", adChar, adParamInput, 20, studentId) CreateParameter("f2", adChar, adParamInput, 20, guardId) CreateParameter("f1", adInteger, adParamInput,, logInId + 1) RstRecordSet.Open "laptopLoggedInLoggedOutInfo", conConnection Private Sub Command1_Click()ĭim conConnection As New ADODB.ConnectionĬonConnection.ConnectionString = "Provider=.12.0 Data Source=" & _Īpp.Path & "\" & "Database.accdb Mode=Read|Write"ĬonConnection.CursorLocation = adUseClient If you can't understand my code or if it is wrong, please show me a working example. Here's my code based on all my research from different websites. My problem is there is no helpful example of writing data (including current date & time) from a form to an Access database.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |