473,379 Members | 1,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Problem with passing a string to a function

Hi,

I have a number of buttons on a form which run mailmerges. Next to
each button is a text box/control that the user enters a date into when
the letter was created/merged. When the user presses the button it
calls a function called LetterMerge to carry out a mail merge.

Here is the on click property for one of the buttons:

Private Sub cmdConsultationEWO_Click()

Dim strLetterDateSentName As String
strLetterDateSentName =
Forms![frmCase]![frmIncident]![frmIncidentPerpetrator]![LetterDateSentConsultEWO].ControlSource
Call LetterMerge("Consultation-EWO.doc", strLetterDateSentName)
End Sub

The control source is called: LetterDateSentConsultEWO for this
particular text box/control to the right of the button.

The function LetterMerge is below:

Public Function LetterMerge(strLetterToMerge As String,
strLetterDateSent As String)
Dim objWord As Word.Document
Dim strMergeDoc As String
Dim strPath As String
Dim msg0 As String
Dim msg1 As String
Dim msgErr As String
Dim strIncidentID As String

On Error Resume Next

If IsNull(strLetterDateSent) Then
MsgBox "You must enter a date for this letter before it can be
generated.", vbExclamation, "Empty Date Field"
strLetterDateSent.SetFocus
End
End If

DoCmd.OpenForm "frmMergingLettersPleaseWait"
DoCmd.RepaintObject acForm, "frmMergingLettersPleaseWait"

strPath = FixPath(CurrentProject.Path)
strPath = strPath & "MergeLetters\"
strMergeDoc = strPath & strLetterToMerge
strIncidentID = [Forms]![frmCase]![frmIncident]![IncidentID]

Set objWord = GetObject(strMergeDoc, "Word.Document")

objWord.Application.Visible = True

objWord.MailMerge.OpenDataSource _
Name:=CurrentDb.Name, _
LinkToSource:=True, _
Connection:="QUERY qryLetterMerge", _
SQLstatement:="SELECT * FROM [qryLetterMerge] WHERE
[IncidentID] =" & strIncidentID

objWord.MailMerge.Execute
objWord.Close

DoCmd.Close acForm, "frmMergingLettersPleaseWait"
End Function

What I need is a check to see if the control: LetterDateSentConsultEWO
is null and if it is display a message to the user, otherwise continue
with the merge.

The problem is this, in the line: If IsNull(strLetterDateSent) Then

The string looks like "LetterDateSentConsultEWO" and it jumps straight
to end if when the field is empty. If I use: If
IsNull(LetterDateSentConsultEWO ) Then

It works OK.

It doesn't seem to like the use of a variable or the fact that it has
quotes around it.

I want to use it this way so that I don't have to put the following in
each of the onclick properties for each button.:

If IsNull(Name Of The Control) Then
MsgBox "You must enter a date for this letter before it can be
generated.", vbExclamation, "Empty Date Field"
Name Of The Control.SetFocus
End
End If
Cheers for any help that can be offered.

Regards.

Nov 13 '05 #1
3 2999
ca**********@newcastle.gov.uk wrote:
Hi,

I have a number of buttons on a form which run mailmerges. Next to
each button is a text box/control that the user enters a date into when
the letter was created/merged. When the user presses the button it
calls a function called LetterMerge to carry out a mail merge.

Here is the on click property for one of the buttons:

Private Sub cmdConsultationEWO_Click()

Dim strLetterDateSentName As String
strLetterDateSentName =
Forms![frmCase]![frmIncident]![frmIncidentPerpetrator]![LetterDateSentConsultEWO].ControlSource
Call LetterMerge("Consultation-EWO.doc", strLetterDateSentName)
End Sub

The control source is called: LetterDateSentConsultEWO for this
particular text box/control to the right of the button.

The function LetterMerge is below:

Public Function LetterMerge(strLetterToMerge As String,
strLetterDateSent As String)
Dim objWord As Word.Document
Dim strMergeDoc As String
Dim strPath As String
Dim msg0 As String
Dim msg1 As String
Dim msgErr As String
Dim strIncidentID As String

On Error Resume Next

If IsNull(strLetterDateSent) Then
MsgBox "You must enter a date for this letter before it can be
generated.", vbExclamation, "Empty Date Field"
strLetterDateSent.SetFocus
End
End If

DoCmd.OpenForm "frmMergingLettersPleaseWait"
DoCmd.RepaintObject acForm, "frmMergingLettersPleaseWait"

strPath = FixPath(CurrentProject.Path)
strPath = strPath & "MergeLetters\"
strMergeDoc = strPath & strLetterToMerge
strIncidentID = [Forms]![frmCase]![frmIncident]![IncidentID]

Set objWord = GetObject(strMergeDoc, "Word.Document")

objWord.Application.Visible = True

objWord.MailMerge.OpenDataSource _
Name:=CurrentDb.Name, _
LinkToSource:=True, _
Connection:="QUERY qryLetterMerge", _
SQLstatement:="SELECT * FROM [qryLetterMerge] WHERE
[IncidentID] =" & strIncidentID

objWord.MailMerge.Execute
objWord.Close

DoCmd.Close acForm, "frmMergingLettersPleaseWait"
End Function

What I need is a check to see if the control: LetterDateSentConsultEWO
is null and if it is display a message to the user, otherwise continue
with the merge.

The problem is this, in the line: If IsNull(strLetterDateSent) Then

The string looks like "LetterDateSentConsultEWO" and it jumps straight
to end if when the field is empty. If I use: If
IsNull(LetterDateSentConsultEWO ) Then

It works OK.

It doesn't seem to like the use of a variable or the fact that it has
quotes around it.

I want to use it this way so that I don't have to put the following in
each of the onclick properties for each button.:

If IsNull(Name Of The Control) Then
MsgBox "You must enter a date for this letter before it can be
generated.", vbExclamation, "Empty Date Field"
Name Of The Control.SetFocus
End
End If
Cheers for any help that can be offered.

Regards.


Very sloppy coding and logic.

Why are you adding confusion to your calls? Why can't you pass a date
field as a date? Or as a variant?
Public Function LetterMerge(strLetterToMerge As String,
datLetterDateSent As Date)
would be more preferable.
Then you have this weird construct.
If IsNull(strLetterDateSent) Then
MsgBox "You must enter a date for this letter before it can be
generated.", vbExclamation, "Empty Date Field"

'what are you setting Focus for at this point. And why would it
'work? It wasn't sent to the function as a control.
strLetterDateSent.SetFocus

'what the heck is this?
End
End If

Why not have an Exit Function instead so it doesn't execute further
statements below it?

Have you heard of the function IsDate()?
Nov 13 '05 #2
Hi,

Thanks for your comments.

I am not trying to pass the date in the control to the function I am
trying to pass the name of the control to see if it is empty. I am
setting the focus to it if it is empty that is why I have the
strLetterDateSent.SetFocus. If I use: IsNull(LetterDateSentConsultEWO
) Then along with LetterDateSentConsultEWO .SetFocus - it works OK as
it is using the control name.

Any helpful suggestions would be appreciated regarding my sloppy code
and logic.

Cheers.

Nov 13 '05 #3
<ca**********@newcastle.gov.uk> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

Thanks for your comments.

I am not trying to pass the date in the control to the function I am
trying to pass the name of the control to see if it is empty. I am
setting the focus to it if it is empty that is why I have the
strLetterDateSent.SetFocus. If I use: IsNull(LetterDateSentConsultEWO
) Then along with LetterDateSentConsultEWO .SetFocus - it works OK as
it is using the control name.

Any helpful suggestions would be appreciated regarding my sloppy code
and logic.


I would suggest passing the form to the procedure (using "Me"), then you
could test the control thusly:

Public Function MyFunction(frm as Form)

If IsNull(frm.txtMyTextBox) Then ... etc

Just my 2p worth.

Keith.
www.keithwilby.com
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Shaun Fleming | last post by:
I've been trying to make this simple script compatible across various browsers. It works for IE 6.0 and NS 7 but doesnt work with Opera (I have version 7.11). This is what is supposed to happen:...
6
by: lenny | last post by:
Hi, I've been trying to use a Sub or Function in VBA to connect to a database, make a query and return the recordset that results from the query. The connection to the database and the query...
2
by: collinm | last post by:
hi i got some error with parameter my searchFile function: char *tmp; int size; ....
1
by: Craig | last post by:
I have added a 'Textboxes (A)' to my UI installer project along with a custom action to pass the value back to a class I've written to override the void Install function. As long as the text is...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
15
by: Peter Afonin | last post by:
Hello, I'm struggling with the string conversion to MD5 which I've never user before. I have a string that I need to encode which looks approximately like this: ...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
26
by: the.tarquin | last post by:
Okay, this one has me totally baffled. I have a function, getParsedKey(char* key, char* returnString). I pass in the key I want, it retrieves it from a data structure and puts the value in...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
2
by: Shashank | last post by:
Hi, I am passing a tcpdump filter to a function which compiles the filter using pcap_compile and then sets it. Here is the filter, ip>=0x0000 and ip <=0xd9295a3 and ip >=0x0000 and ip...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.