I am new to MS Access and I am trying to create a simple Macro with a call to VBA code.
the VBA code here is a sample (which also doesn't run)
Public Function RunImport()
Dim N As Integer
Dim Message1, Message2, Title, Default1, Default2, JulianSD, JulianED
Message1 = "Enter Julian Start Date"
Message2 = "Enter Julian End Date"
Title = "User Input Section"
Default1 = "17365"
Default2 = "17000"
JulianSD = InputBox(Message1, Title, Default1)
JulianED = InputBox(Message2, Title, Default2)
End Function
do you think you might be able to locate an issue here?
Thanks!
PS. I am using Version 14.0.7177.500 (32-bit). it wasn't my choice.. (if it were, I wouldn't be using access.. :p)
The most common use of a function is to return a value or values. Your function does not appear to be returning any value. At the end of a function you would normally have a line of code that says what value the function will return, for example....
RunImport = JulianSD - JulianED
End Function
A line like this would usually be inserted before the 'End Function' line. However if your intention is not to return a value, but instead you just want to run a vba macro, perhaps you need to change your function to a Sub routine...
Public Sub RunImport()
'code goes here
End Sub
Related
I use the following function to send commands to an instrument and receive the query results back and it works fine.
Public Function InstrSndRcv(cmd2Send As String) As String
MyInstrument.WriteLine(cmd2Send)
' if myTimeout(desierdtime) then
'Return "my string"
'else
Dim qryRetrn As String = MyInstrument.ReadLine()
Return qryRetrn
'End if
End Function
I have commented out the conditional functionality I need in terms of a subroutine myTimeout(desiredtime). How to implement it in VB.NET?
The idea is to return "my string" in case the instrument does not respond to a query command. May be there is a better way to do this...
I have a form that keeps track of assigned patient equipment. I have it set so that any changes made to text fields on the form automatically move down to the "comments" section of the form (this is done so that any changes made are documented in case the user forgets to manually document changes). I have a sub that I wrote that accomplishes this that I am currently calling for every single text field. This works but is messy.
Is there a way to apply the sub to all the fields in one procedure without calling it for every individual field? Code is below, please let me know if I can clarify anything.
Private Sub pPEMoveValue(sField)
'Moves the old field value down to the comments section automatically
Dim sOrigValue As String
Dim sCommentValue As String
sOrigValue = sField
sCommentValue = Nz(mPEComments, "")
Me.mPEComments = sCommentValue & vbNewLine & sOrigValue
End Sub
Private Sub sPEBatCharger_Dirty(Cancel As Integer)
pPEMoveValue (Nz(Me.sPEBatCharger.OldValue, ""))
End Sub
This is the solution I came up with to do what you are looking to do. I took advantage of the MS Access Tag system. You can add tags to your controls so you can sort of "Group" them.
First put the form in design view and adjust the tag for all of the fields you want to record to say "Notes".
Then in the Form's BeforeUpdate even you would add this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Call FindControlsForComments(Me.Form)
End Sub
Then you would use this function to find any fields that have the "Notes" tag and run it through the function you created:
Public Function FindControlsForComments(frm As Form)
Dim ctrl As Access.Control
For Each ctrl In frm
'If the control is tagged for notes
If ctrl.Tag = "Notes" Then
'If the old value is different than the current value
If Nz(ctrl.OldValue, "") <> Nz(ctrl.Value, "") Then
'Add to comment
Call pPEMoveValue(Nz(ctrl.Value, ""))
End If
End If
Next ctrl
End Function
You may have to adjusted this slightly to work with your system but this has worked well for me.
Okay, so I am working on a small scripting language using a VB Console Application.
I want the user to input "say('something')" and it calls the function I made named "say", is there a way to call the function and still use the following code:
Module Module1
Sub say(sayline)
Console.WriteLine(sayline)
End Sub
Sub Main()
Dim cmd As String
Console.WriteLine(">")
Do
Console.Write("")
cmd = Console.ReadLine()
If cmd IsNot Nothing Then cmd
Loop While cmd IsNot Nothing
End Sub
End Module
No, you cannot just call a method from user's string. You need to interpret the entered data.
First, you need to split your method name and arguments so that entered "say('something')" will transform to say and something. Remember that user can enter wrong data and you need to check if this call is correct - it's all about syntactic and lexical analysis. I hope you understand how to do this because it is pretty difficult.
Then, you need to check if you have a method called say. In case of plain and simple structure, switch construction will be enough. If your have such method, then pass something argument to this method. Else, output something like "unknown method".
If you wanted to call the method say upon typing the word say(something) and display the word something, then you can just have a certain condition that if the user types the word say within the input then call say method else, do whatever you want to do under else portion. Parse the input and omit the word say from the input and display it then.
You can have your code this way for example. (I just copied your code and added some codes to meet what you wanted... in my understanding)
Module Module1
Sub say(ByVal sayline)
Console.WriteLine(sayline)
End Sub
Sub Main()
Dim cmd As String
Do
Console.Write("> ")
cmd = Console.ReadLine()
Try
If cmd IsNot Nothing And cmd.Substring(0, 3).ToUpper().Equals("SAY") Then
say(parseInput(cmd))
End If
Catch ex As Exception
Console.WriteLine("message here")
End Try
Loop While cmd IsNot Nothing
End Sub
Function parseInput(ByVal cmd As String) As String
Dim input As String = ""
For index As Integer = 3 To cmd.Length - 1
If Char.IsLetter(cmd) Then
input += cmd.Substring(index, 1)
Else
input = input
End If
Next
Return input
End Function
End Module
I am not familiar to OpenOffice Basic, but I need a simple macro code for setting document variables (user defined fields accessable under "field commands"/"Variables" in GUI) that I can assign to a button.
Example: I create a button A calling macro sub SetDocVar on a click, while SetDocVar sets the document variable/field MyField to string value "Test".
How does it work?
This is how it works:
Sub SetDocVar
tmp = ThisComponent.TextFields.createEnumeration
Do While tmp.hasMoreElements
tf = tmp.nextElement
if tf.supportsService("com.sun.star.text.TextField.User") And _
tf.TextFieldMaster.Name = "MyField" then
tf.TextFieldMaster.Content = "Test"
end if
Loop
ThisComponent.TextFields.refresh
End Sub
I have written the following query:
Private Sub Size_Sqft_BeforeUpdate(Cancel As Integer)
Me!Size_Sqft = Nz(Me!Size_Sqft, 0)
End Sub
But while removing the zero in the field to make it null, I am getting the following error:
Runtime error 2115
Macro and function set to before update and validation rule property for this field is preventing manual data entry screen for company from saving the data in the field.
You have to put that code in the AfterUpdate event of that field.
I know this is an old thread, and has already been answered, but there is another solution that doesn't require several writes back to your database. I'm adding it in case someone else comes across this question.
Private Sub ControlName_BeforeUpdate(Cancel as integer)
If isValid(Me.ControlName.Value) = False Then
Cancel = True
Me.ControlName.Undo
End If
End Sub
Private Function isValid(ByVal...) as boolean
' validate control value here
End Function