Is there a way to autoincrement default value in text box assuming data type as text without having to create table? i needed this to happen when form loads.
This cannot be done when a form is opened, so you would need to call this Sub (located in a Module) to open the form:
Sub ChangeDefaultValue()
DoCmd.Close acForm, "Form1"
DoCmd.OpenForm "Form1", acDesign, , , , acHidden
Dim f As Form
Set f = Forms("Form1")
Dim dv As Integer
dv = f.Controls!Text0.DefaultValue
f.Controls!Text0.DefaultValue = dv + 1
DoCmd.Close acForm, "Form1", acSaveYes
DoCmd.OpenForm ("Form1")
End Sub
You will need to change the form name "Form1" and TextBox name "Text0" to your names.
Related
I use the following code to open the form on a specific record or in add data mode:
Private Sub Open_Click()
Dim recordID As Integer
recordID = Me.ID
'Debug.Print Me.ID
'Stop
If Me.ID <> "" Then
DoCmd.OpenForm "Add Task", acNormal, , "ID=" & recordID, acFormEdit
Else
DoCmd.OpenForm "Add Task", acNormal, , , acFormAdd
End If
End Sub
Even though Access understands and prints my ID, it then fails to open the form at that record. This happens only when I add records by importing them from Excel.
Could you please help?
Me.ID is an integer, and you compare it to a string. This triggers a type mismatch runtime error with Option Explicit on, but just returns Null with Option Explicit off, which is seen as False in your If statement.
Please add Option Explicit, and respect data types (learn about Option Explicit here.
The proper code:
Private Sub Open_Click()
Dim recordID As Integer
If Not IsNull(Me.ID) Then
'Note that IsNull(RecordID) would always return true, as integers can't be Null.
recordID = Me.ID
DoCmd.OpenForm "Add Task", acNormal, , "ID=" & recordID, acFormEdit
Else
DoCmd.OpenForm "Add Task", acNormal, , , acFormAdd
End If
End Sub
Its been a long time since I have used coded in VBA, and I am rusty...
Trying to look at a field, which is populated by a combo box, within a table to see if it is null or not. Based upon the field having value or not, the code determines which form to open.
[table].field
Form1
Form2
here is what I have attempted to no avail
If DLookup("[field]", "table", IsNull) Then
DoCmd.OpenForm "form1", acNormal
End If
If DLookup("[field]", "table", NotIsNull) Then
DoCmd.OpenForm "form2", acNormal
I can't get this to run without errors. Help!
Do you mean you've got a combo-box on a form, the combo-box gets its values from a table. If the selected value is blank then open one form, if it's got a value open another form?
Based on a two column Combo-Box (column 0 is hidden key value, column 1 holds the value we're after):
Private Sub Combo0_AfterUpdate()
With Me.Combo0
If .Column(1) = "" Then
DoCmd.OpenForm "Form2", acNormal
Else
DoCmd.OpenForm "Form3", acNormal
End If
End With
End Sub
Based on a 1 column combo:
Private Sub Combo0_AfterUpdate()
With Me.Combo0
If IsNull(.Value) Then
DoCmd.OpenForm "Form2", acNormal
Else
DoCmd.OpenForm "Form3", acNormal
End If
End With
End Sub
I have working on forms, after i filled all the fields and Click on Save button, all fields have been saved but the text fields was not empty, all entered text still present, How do removed those text after hit on the save button
Go to a new record:
DoCmd.GoToRecord , , acNewRec
May be you would like to add the following in the click on save button event before "End Sub"
Me.TextboxName.Value = Null
Try this routine linked to a button you want to click to clear all textboxes in an unbound form:
Private Sub CleanAllFieldsButton_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then ctl = Null
Next
Set ctl = Nothing
End Sub
Private Sub Command47_Click()
On Error Resume Next
Me.Dirty = False 'Attempt to save the record
If Err.Number = 0 Then
'Only enter a new record, leaving other newly-added record accessible
DoCmd.GoToRecord , , acNewRec
Else MsgBox Err.DESCRIPTION, vbCritical, "Error"
End If
End Sub
Try this is in your save records click event.
I have a form in datasheet view, on which the first column displays the autonumber primary key of the records displayed.
This field appears as a hyperlink which is used to open a form to manipulate the data of the record.
I would like this hyperlink to open one of two different forms, depending on whether a particular field is null or not.
This is the code I use to open the form to the relevant record
Private Sub ISP_ID_Click()
TempVars.Add "CurrentRecord", ISP_ID.Value
DoCmd.OpenForm "frmModifyISP", acNormal, "", "[ISP_ID]=[TempVars]![CurrentRecord]", acEdit, acNormal
End Sub
I would like to open a form named "frmModifyISP_Address" if a field called "AddressID" for the current record is not null.
Any help with this would be greatly appreciated.
How about:
Private Sub ISP_ID_Click()
If IsNull(Me!AddressID.Value) Then
DoCmd.OpenForm "frmModifyISP_Address"
Else
TempVars.Add "CurrentRecord", ISP_ID.Value
DoCmd.OpenForm "frmModifyISP", acNormal, "", "[ISP_ID]=[TempVars]![CurrentRecord]", acEdit, acNormal
End If
End Sub
Or lookup the value:
Private Sub ISP_ID_Click()
If IsNull(DLookup("AddressID", "YourTable", "ID = " & Me!ISP_ID.Value & "")) Then
DoCmd.OpenForm "frmModifyISP_Address"
Else
TempVars.Add "CurrentRecord", ISP_ID.Value
DoCmd.OpenForm "frmModifyISP", acNormal, "", "[ISP_ID]=[TempVars]![CurrentRecord]", acEdit, acNormal
End If
End Sub
When a button is pressed I want to open a form at a specific tab.
on click event I have:
DoCmd.OpenForm "MAIN_USER_FORM", acNormal, , "App_Ref = " & Me.App_Ref, , , "PRB"
On the open event of the form I have:
If Me.OpenArgs = "PRB" Then
Me.PRB_Validation.SetFocus
End If
PRB_Validation is the name of the tab in the MAIN_USER_FORM I wish to open.
I've searched forms, I just can't get it to work any help would be great.
Thanks in advance.
All you need is to check the OpenArgs in the form's OnLoad event, and set the TabCtontrol's value to the index of the page you want to show, like this:
Private Sub Form_Load()
If OpenArgs = "PRB" Then
TabCtl0.Value = PagePRB.PageIndex
End If
End Sub
I made an example accdb to show the complete setup.
In case if anyone is looking for code where you have a button on another form and want to open Main form from that and also take user to a particular Tab.
Private Sub YourButton_Click()
On Error GoTo Err_YourButton_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "YourFormName"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Forms![YourFormName]!YourPage.SetFocus
Exit_YourButton_Click:
Exit Sub
Err_YourButton_Click:
MsgBox Err.Description
Resume Exit_YourButton_Click
End Sub