Private Sub Form_Open(Cancel as Integer) - ms-access

I have a main form with all of the selection criteria. If someone selects a year, like 2015, inadvertantly, there is NO data in the SQL Server for that year. When a button is pushed to open a subsequent Form, it will produce a Run_Time Error 2105. What would I need to add to the following code to alleviate the aforementioned error:
Private Sub Form_Open(Cancel As Integer)
Me.AllowEdits = True
DoCmd.GoToRecord , , acFirst
End Sub

Something along the lines of
'test for numeric
If IsNumeric(UserForm1.ComboBox1.Value) Then
'convert to long before comparing to year
If CLng(UserForm1.ComboBox1.Value) <= Year(Now) Then
DoCmd.GoToRecord , , acFirst
End If
End If

Related

How to pass multiple values of a textbox from one form to another form in access

I have two forms for Purchase and Sales in my inventory DB. Sometimes I need to show a purchase to sale directly in the purchase form. I have seen other questions like this which couldn't solve my problems. Also I tried like the following:
Option Compare Database
Private Sub Command25_Click()
MsgBox "Are you sure to show this purchase as direct sale?", vbYesNo, "Direct Sale"
If vbYes Then
DoCmd.OpenForm "SF02PurchasetoSale", acNormal, , , , , DirectSale
End If
End Sub
Private Sub DirectSale()
Forms![SF02PurchasetoSale].[TxnDate] = Me.TxnDate.Value
Forms![SF02PurchasetoSale].[Hub] = Me.Hub.Value
Forms![SF02PurchasetoSale].[Code] = Me.Code.Value
Forms![SF02PurchasetoSale].[PurchasePrice] = Me.PurchasePrice.Value
Forms![SF02PurchasetoSale].[QtySold] = Me.QuantityPurchased.Value
End Sub
This shows the "compile error: Expected Function or variable" on the openargs of DirectSale. I really need some help here. Thanks...
OpenArgs is to pass values to form or report specified in OpenForm or OpenReport command. Consider:
DoCmd.OpenForm "SF02PurchasetoSale", acNormal, , , acFormAdd, , "DirectSale"
Then code behind SF02PurchasetoSale:
Private Sub Form_Load()
With Me
If .OpenArgs = "DirectSale" Then
.[TxnDate] = Forms!Purchases.TxnDate
.[Hub] = Forms!Purchases.Hub
.[Code] = Forms!Purchases.Code
.[PurchasePrice] = Forms!Purchases.PurchasePrice
.[QtySold] = Forms!Purchases.QuantityPurchased
End If
End With
End Sub

Next Button that cycles back to first on Access Form

I've been trying a couple things to create a button that navigates back to the first record you are at the last record. I get the "can't go to specified record" error. Here are the two styles of code I have tried for this button:
Private Sub Command161_Click()
On Error Resume Next
If Me.CurrentRecord = acLast Then
DoCmd.GoToRecord , , acFirst
Else
DoCmd.GoToRecord , , acNext
End If
End Sub
Private Sub Command161_Click()
With Me.Recordset
If .AbsolutePosition = .RecordCount - 1 Then
DoCmd.GoToRecord , , acFirst
Else
DoCmd.GoToRecord , , acNext
End If
End With
End Sub
The goal is to loop back around to the first record without allowing the user to create a new record. I've tried this code with "allow additions" set to both yes and now, with the same result. Any help would be appreciated.
I would suggest defining a private predicate function within your form module which returns a boolean value depending on whether or not the active form is displaying the last record in its Record Source.
Such a function might be written:
Private Function LastRecordP() As Boolean
With Me.RecordsetClone
If Not .EOF Then
.MoveLast
.MoveFirst
LastRecordP = Me.CurrentRecord = .RecordCount
End If
End With
End Function
Your OnClick event handler for your button control could then be written more succinctly as:
Private Sub Command161_Click()
If LastRecordP Then
DoCmd.GoToRecord , , acFirst
Else
DoCmd.GoToRecord , , acNext
End If
End Sub
Alternatively, you could allow the function to accept a Form object as an argument and evaluate such function using the Me keyword, e.g.:
Private Function LastRecordP(Frm As Form) As Boolean
With Frm.RecordsetClone
If Not .EOF Then
.MoveLast
.MoveFirst
LastRecordP = Frm.CurrentRecord = .RecordCount
End If
End With
End Function
Private Sub Command20_Click()
If LastRecordP(Me) Then
DoCmd.GoToRecord , , acFirst
Else
DoCmd.GoToRecord , , acNext
End If
End Sub

Record navigation on a form

So I have been tasked with creating a tracking database & I am having an issue which is driving me crazy.
I have created an input form and for ease of use I need to create custom navigation buttons, again I have completed this and they work fine.
I have added code to the current event to disable the buttons if at the first record / last record - again this works fine.
This is where I have gotten stuck, as soon as I disable the built in navigation buttons it disables the custom buttons I have created. If I remove the current event code, again they work but not how I intend.
I would appreciate any help you could provide as I am still new to learning Access & I am sure this is a simple fix & something I am not doing correct - but I just cannot figure it out, I have included my code below.
Thank you in advance for your assistance.
Option Compare Database
Option Explicit
Private Sub cmdBack_Click()
On Error Resume Next
DoCmd.GoToRecord , , acPrevious
End Sub
Private Sub cmdNew_Click()
On Error Resume Next
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub cmdNext_Click()
On Error Resume Next
DoCmd.GoToRecord , , acNext
End Sub
Private Sub Form_Current()
On Error Resume Next
If Me.CurrentRecord = 1 Then
Me.cmdBack.Enabled = False
Else
Me.cmdBack.Enabled = True
End If
If Me.CurrentRecord >= Me.Recordset.RecordCount Then
Me.cmdNext.Enabled = False
Else
Me.cmdNext.Enabled = True
End If
If Me.NewRecord Then
Me.cmdNew.Enabled = False
Else
Me.cmdNew.Enabled = True
End If
End Sub
You can replace your DoCmd. calls with navigation using the form recordset.
Me.Recordset.MoveNext 'alternative to DoCmd.GoToRecord , , acNext
Me.RecordSet.MovePrevious 'alternative to DoCmd.GoToRecord , , acPrevious
Me.RecordSet.AddNew 'alternative to DoCmd.GoToRecord , , acNewRec
Thank you very much for the updated code - I had to add the following line to get them to work as I intented though.
Me.RecordsetClone.MoveLast

Automatically Close Form After Certain Idle Time

I am having a little bit problem on my access form.
I have 2 forms:
menu-form
input-form
After input-form has been idle for one minute, I want to close it and go back to the menu-form.
This is my code that isn't working.
Public ExpireTime As Date 'expiration time-date value
Sub ResetExpiration()
If ExpireTime <> 0 And ExpireTime > Now Then
Application.OnTime ExpireTime, "FormExpire", schedule:=False
End If
ExpireTime = Now + 1 / 1440#
Application.OnTime ExpireTime, "FormExpire", schedule:=True
End Sub
I also created a macro with this in it.
Sub FormExpire()
Unload input-form
End Sub
You need to set the form.Timer to 60000 (that is 1 minute) then use on OnTimer event to check if some properties have changed. Below I quickly wrote something to give you an idea, but it is not complete nor tested.
Option Compare Database
Option Explicit
Dim isIdle As Boolean
Private Sub Form_LostFocus()
'you can use this event also
End Sub
Private Sub Form_Timer()
Dim ctlName As String, wasDirty As Boolean
If ctlName = vbNullString Then ctlName = Me.ActiveControl.Name
If Me.ActiveControl <> ctlName Then isIdle = False
If wasDirty <> Me.Dirty Then isIdle = False
'more checks....
If isIdle Then DoCmd.Close acForm, Me.Name
End Sub
You've got a couple of potential issues here.
First and foremost, that's not how you close a form.
This:
Sub FormExpire()
Unload input-form
End Sub
Should look something more like this:
Sub FormExpire()
DoCmd.Close acform, Me.Name
DoCmd.OpenForm "menu-form"
End Sub
Your second issue is that I don't believe the Application.Ontime method is available from Access. MSDN says it's an Excel function, and I can't find it in the Access Documentation. I'd personally be interested in seeing you make your method work with the Access form Timer Event.
I'm not sure it matters, but I think this is what you were trying to do.
Public ExpireTime As Date 'expiration time-date value
Private Sub InputForm_Timer()
If ExpireTime <> 0 And ExpireTime > Now Then
Call FormExpired
Else
' reset timer
ExpireTime = Now + 1 / 1440#
End If
End Sub
I'm not sure this method would work without also including #iDevelop's answer. I just wanted to help clarify where you were going wrong.
I realize that this thread is aged but I recently experienced a need to close forms from a timer event and came across the answers here. My situation was slightly different as I had several forms to close so here is my solution.
Private Sub Form_Timer()
Dim daysRemaining As Integer
Dim endDate As Date
endDate = "4/15/2021"
daysRemaining = DateDiff("d", Now, endDate)
MsgBox ("This is an evaluation version of the application. You have " & Str(daysRemaining) & " days of use remaining")
If daysRemaining < 1 Then
Close_All
End If
End Sub
Private Sub Close_All()
For Each myForm In CurrentProject.AllForms
strFormName = myForm.name
DoCmd.Close acForm, strFormName
Next
End Sub

Access: Move to next record until EOF

I need to loop through a form by moving to the next record in the recordset.
I am using the Form_Current event to loop thru.
I have used a couple of statements and have different outcomes.
This one sometimes crashes and gives the error message: "You can't go to the specified record."
DoCmd.GoToRecord , , acNext
This one only goes upto 72 records and stops.
DoCmd.RunCommand acCmdRecordsGoToNext
This one only goes upto 129 records and stops.
Me.Recordset.MoveNext
Trying to find an instruction that will go to the next record untill it reaches the End of File.
I am using Access 2010 (Access 2002 -2003 file format mdb) as the front end. The recordsource is a SQL Server 2008 linked View.
To loop from current record to the end:
While Me.CurrentRecord < Me.Recordset.RecordCount
' ... do something to current record
' ...
DoCmd.GoToRecord Record:=acNext
Wend
To check if it is possible to go to next record:
If Me.CurrentRecord < Me.Recordset.RecordCount Then
' ...
End If
If (Not IsNull(Me.id.Value)) Then
DoCmd.GoToRecord , , acNext
End If
Hi,
you need to put this in form activate, and have an id field named id...
this way it passes until it reaches the one without id (AKA new one)...
I have done this in the past, and have always used this:
With Me.RecordsetClone
.MoveFirst
Do Until .EOF
If Me.Dirty Then
Me.Dirty = False
End If
.MoveNext
Me.Bookmark = .Bookmark
Loop
End With
Some people would use the form's Recordset, which doesn't require setting the bookmark (i.e., navigating the form's Recordset navigates the form's edit buffer automatically, so the user sees the move immediately), but I prefer the indirection of the RecordsetClone.
Set rs = me.RecordsetClone
rs.Bookmark = me.Bookmark
Do
rs.movenext
Loop until rs.eof
If you want cmd buttons that loop through the form's records, try adding this code to your cmdNext_Click and cmdPrevious_Click VBA.
I have found it works well and copes with BOF / EOF issues:
On Error Resume Next
DoCmd.GoToRecord , , acNext
On Error Goto 0
On Error Resume Next
DoCmd.GoToRecord , , acPrevious
On Error Goto 0
Good luck! PT
Keeping the code simple is always my advice:
If IsNull(Me.Id) = True Then
DoCmd.GoToRecord , , acNext
Else
DoCmd.GoToRecord , , acLast
End If
Add This Code on Form Close Event whether you add new record or delete, it will recreate the Primary Keys from 1 to Last record.This code will not disturb other columns of table.
Sub updatePrimaryKeysOnFormClose()
Dim i, rcount As Integer
'Declare some object variables
Dim dbLib As Database
Dim rsTable1 As Recordset
'Set dbLib to the current database (i.e. LIBRARY)
Set dbLib = CurrentDb
'Open a recordset object for the Table1 table
Set rsTable1 = dbLib.OpenRecordset("Table1")
rcount = rsTable1.RecordCount
'== Add New Record ============================
For i = 1 To rcount
With rsTable1
rsTable1.Edit
rsTable1.Fields(0) = i
rsTable1.Update
'-- Go to Next Record ---
rsTable1.MoveNext
End With
Next
Set rsTable1 = rsTable1
End Sub