D
dick_grayson
I have a form based on a table with five fields: ID (text field set as the
primary key), Status (text combo field limited to two unique entries, either
Own or Sold), PurchasePrice (currency field), SalePrice (currency field), and
Net (currency field). I am relatively unfamiliar with Visual Basic, so as a
beginning exercise I am trying to write a function that simply takes the
difference between the PurchasePrice and SalePrice fields, constrained by the
values present in Status and SalePrice, and enters the resulting value in the
Net field. I have the following code saved as a module named Module1 that
contains the function:
Option Compare Database
Option Explicit
Public Function CalcNet(Purchase As Variant, Sale As Variant, Status As
Variant) As Currency
Dim Result As Currency
If InStr(1, Status, "O") = 1 Then
Result = 0
Else
If IsNull(Sale) Then
Result = 0
Else
Result = Sale - Purchase
End If
End If
CalcNet = Result
Exit Function
End Function
I then have the following code as part of the form in which these fields
reside:
Option Compare Database
Option Explicit
Private Sub Form_AfterUpdate()
NetProfit
End Sub
Private Sub Form_Current()
NetProfit
End Sub
Private Sub PurchasePrice_AfterUpdate()
NetProfit
End Sub
Private Sub SalePrice_AfterUpdate()
NetProfit
End Sub
Private Sub NetProfit()
Me!Net = CalcNet(Me!PurchasePrice, Me!SalePrice, Me!Status)
End Sub
The function calculates the results exactly as desired. However, the
problem I am having is that I cannot view any other records other than only
the first record when I open the form. The navigation bar at the bottom of
the form window shows that I am viewing record 1 of xx, but I cannot go to
any other record.
primary key), Status (text combo field limited to two unique entries, either
Own or Sold), PurchasePrice (currency field), SalePrice (currency field), and
Net (currency field). I am relatively unfamiliar with Visual Basic, so as a
beginning exercise I am trying to write a function that simply takes the
difference between the PurchasePrice and SalePrice fields, constrained by the
values present in Status and SalePrice, and enters the resulting value in the
Net field. I have the following code saved as a module named Module1 that
contains the function:
Option Compare Database
Option Explicit
Public Function CalcNet(Purchase As Variant, Sale As Variant, Status As
Variant) As Currency
Dim Result As Currency
If InStr(1, Status, "O") = 1 Then
Result = 0
Else
If IsNull(Sale) Then
Result = 0
Else
Result = Sale - Purchase
End If
End If
CalcNet = Result
Exit Function
End Function
I then have the following code as part of the form in which these fields
reside:
Option Compare Database
Option Explicit
Private Sub Form_AfterUpdate()
NetProfit
End Sub
Private Sub Form_Current()
NetProfit
End Sub
Private Sub PurchasePrice_AfterUpdate()
NetProfit
End Sub
Private Sub SalePrice_AfterUpdate()
NetProfit
End Sub
Private Sub NetProfit()
Me!Net = CalcNet(Me!PurchasePrice, Me!SalePrice, Me!Status)
End Sub
The function calculates the results exactly as desired. However, the
problem I am having is that I cannot view any other records other than only
the first record when I open the form. The navigation bar at the bottom of
the form window shows that I am viewing record 1 of xx, but I cannot go to
any other record.