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
Related
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
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
So here is the datasheet form
In the combo box I set value based on Employee ID
As of now I looked at .CurrentRecord but that returns the row number not the ID.
I can succesfully pass the .CurrentRecord Value to the form and set that as the ComboBox problem is the number of row is not necessarily equal to the Employee ID
What I want is the User to be able to select a record on the sheet that is an employee and when the User presses the Trainings button it shows the second form with combo box set to that employees ID.
Recording value so it's not lost when button is clicked with timer event, timing is set to 500
Private Sub Form_Timer()
value = Me.EID.value
End Sub
I passed the value using DoCmd.OpenForm last argument.
Private Sub trainings_Click()
On Error GoTo trainings_Click_Err
' _AXL:<?xml version="1.0" encoding="UTF-16" standalone="no"?>
' <UserInterfaceMacro For="show_trainings" xmlns="http://schemas.microsoft.com/office/accessservices/2009/11/application"><Statements><Action Name="OpenForm"><Argument Name="FormName">Employee
' _AXL:Trainings</Argument><Argument Name="WhereCondition">="[ATTUID]=" &"'" &[ATTUID] & "'"</Argument></Action></Statements></UserInterfaceMacro>
DoCmd.OpenForm "Add Training", acNormal, "", "", , acNormal, value
trainings_Click_Exit:
Exit Sub
trainings_Click_Err:
MsgBox Error$
Resume trainings_Click_Exit
End Sub
and at last I read the Argument and set the Combo box value and for the dependent listboxes I called requery
Private Sub Form_Open(Cancel As Integer)
If Not IsNull(Me.OpenArgs) Then
Me.cboEmp = Me.OpenArgs
Me.List14.Requery
Me.List18.Requery
End If
End Sub
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.
Can someone please help me on this find specific record Edit through MS access Forms
I have Frmfind form where I have one filed "ticket#" is a input text box
another filed was button "find"
When I enter ticket# which is primary key for my table. I need get the the specific ticket# record should be opened in FormEdit mode using VBA code...
So I have another form "frmEdit" of specific record which has to be called from frmfind -> specific input..
note: Ticket# is column in my table whcih it is primary to have the ticket#.
Code:
Option Compare Database
Private Sub find_Click()
If IsNull(Me.Text79) Or Me.Text79 = "" Then
MsgBox "You must enter a Ticket #", vbOKOnly, "Required Data"
Me.Text79.SetFocus
Exit Sub
End If
If [Ticket#] = Me.Text79.Value Then
MsgBox "Record found"
DoCmd.Close
DoCmd.OpenForm "frmEdit"
Else
MsgBox "not matching record"
Me.Text79.SetFocus
End If
End Sub
Private Sub Form_Open(cancel As Integer)
'On open set focus to text box
Me.Text79.SetFocus
End Sub
You can use this code:
Private Sub Command112_Click()
Me.txtSeach.SetFocus
On Error Resume Next
DoCmd.OpenForm "frmEdit", , , "TicketNumber = '" & Nz(Me.text79, "") & "'"
End Sub
Note in above if TicketNumber is in fact a number column and not text, then remove the single quotes and use this:
DoCmd.OpenForm "aa", , , "TicketNumber = " & Nz(Me.text79, "")
Then for your message, just place that code in the forms on-open event that has a cancel:
eg:
Private Sub Form_Open(Cancel As Integer)
If IsNull(Me!TicketNumberID) Then
MsgBox "Please enter a valid Ticket #", vbOKOnly, "Required Data"
Cancel = True
End If
End Sub
The above assumes your search column is ticket number.
You can also use dlookup(), or even dcount(). I think above is less code, but:
Dim strWhere As String
strWhere = "TicketNumber = " & Me.text79
If DCount("*", "tblBookings", strWhere) > 0 Then
code for Record found goes here
Else
code reocrd not found code here
End If
So either way should suffice here.