Word in message box appears in a different line - vba

I use the following VBA to open a message box:
Sub Message()
MsgBox ("Do you want create a file on your desktop?" _
& vbCr & " " _
& vbCr & "Once you click yes an unprotected file with all sheets visible will be saved on your desktop and opened. You can immediately start working with this file.")
End Sub
All this works fine.
However, as you can see in the screenshot the word "file" goes in a different line. Is it possible to format the messagebox or change the VBA code so the word "file" does not appear in a different line?

It depends on the resolution of the PC of the user. In my case I even get it like this:
If you want to control the lines, then you should write a custom form. There you would have much better control over the display, and if you play a bit with its properties, you would mimic exactly the MsgBox():
Some sample code, Label1 is a label element:
Private Sub UserForm_Initialize()
Me.Label1 = "Do you want create a file on your desktop?" _
& vbCr & " " _
& vbCr & "Once you click yes an unprotected file with all sheets visible will be saved on your desktop and opened." & _
vbCrLf & "You can immediately start working with this file."
End Sub

Perhaps adjust your code with line breaks to suit. For example:
Sub Message()
MsgBox ("Do you want create a file on your desktop?" _
& vbCr & " " _
& vbCr & "Once you click yes an unprotected file" _
& vbCr & " " _
& vbCr & "with all sheets visible will be saved on your desktop and opened." _
& vbCr & " " _
& vbCr & "You can immediately start working with this file.")
End Sub

Related

DataGridView - Data added does not stay after closing application

I have created a VB windows form Application with text boxes and a datagridview.
The idea is for the user to enter information onto the text boxes, and then click a button to save to the grid view. This works fine, but when closing the application and reopening the data is gone.
The below code is what i use to save the data to the grid view.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rnum As Integer = DataGridView1.Rows.Add()
If Me.CheckBox1.Checked = True Then
Clipboard.SetText("Situation:" & vbNewLine & Me.TextBox1.Text & vbNewLine & vbNewLine & "Actions:" & vbNewLine & Me.TextBox2.Text & vbNewLine & vbNewLine & "Next Steps:" & vbNewLine & Me.TextBox3.Text & vbNewLine & vbNewLine & "Notes:" & vbNewLine & Me.TextBox4.Text)
Else
Clipboard.SetText("Situation:" & vbNewLine & Me.TextBox1.Text & vbNewLine & vbNewLine & "Actions:" & vbNewLine & Me.TextBox2.Text & vbNewLine & vbNewLine & "Next Steps:" & vbNewLine & Me.TextBox3.Text)
End If
DataGridView1.Rows.Item(rnum).Cells("Situation").Value = Me.TextBox1.Text
DataGridView1.Rows.Item(rnum).Cells("Actions").Value = Me.TextBox2.Text
DataGridView1.Rows.Item(rnum).Cells("NextSteps").Value = Me.TextBox3.Text
DataGridView1.Rows.Item(rnum).Cells("SupportingDocuments").Value = Me.TextBox4.Text
MsgBox("Added to Clipboard")
End Sub
The application is used by different users on different machine, so i want the gridview to have saved what that individual user has added.
I know im missing something easy, like adding a dataset but wouldnt this have to be on a static location used by everyone? And i also looked into xml files but cannot seem to find what im looking for.
Any help would be greatly appreciated.
All you have to do is to select your database in Project Explorer and navigate to its properties. In its properties, set "Copy to Output folder = Copy if Newer"
After trying to work around databases, but only seeming to get them static. I worked on using code to create xml files on the user profile, and then loading them.
This Link helped me alot

When DoCmd.OpenForm opens a blank form because no record was found, display a message box that says record is not found

I have a MainMenu where the user can enter a SchoolID in a search bar (txtSearchBar) and when they click on the SearchBySchoolID button, it opens the form, SchoolForm, based on the SchoolID. Sometimes the user clicks the SearchBySchoolID button without entering anything in the txtSearchBar so when the code below is executed, SchoolForm still opens but it is all blank.
What can I add to my code below so that a message box saying "No SchoolID found" pops up instead of bringing the user to a blank SchoolForm when they type nothing into my search bar?
Private Sub SearchBySchoolID_Click()
Dim txtSearchBar As String
On Error GoTo ErrorIDSearch
DoCmd.OpenForm "SchoolForm", , , "SchoolID = " & ("""" &
Me.txtSearchBar.Value & """"), acFormReadOnly
ExitErrorIDSearch:
Exit Sub
ErrorSIDSearch:
If Err.Number = 3075 Then
MsgBox "Please enter a valid SchoolID."
Else
MsgBox "The following error has occured:" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & vbCrLf & _
"Error Description: " & Err.Description & vbCrLf & vbCrLf & , _
vbCritical, "An Error has Occured!"
Resume ExitErrorIDSearch
End If
End Sub
You have two choices
Either put your event handling in the OnOpen event of the form named SchoolForm (and maybe use OpenArgs or an invisible textbox to set some kind of Status on that form so it knows where it was opened from, and why)
or simpler:
Check for existence of the SchoolID in the table before attempting to open SchoolForm
Could use a DLookup or a DCount
e.g.
If DCount("SchoolID", "YourTableOrQueryForSchools", "SchoolID=" & """" & Me.txtSearchBar.Value & """") = 0 Then
MsgBox "Please enter a valid SchoolID.", 64, "Try Again"
Me.txtSearchBar = ""
Me.txtSearchBar.Setfocus
Else
DoCmd.OpenForm "SchoolForm", , , "SchoolID = " & """" & Me.txtSearchBar.Value & """", acFormReadOnly
End If

How to create a comment box with time and date stamp in Access using VBA

Completely new to VBA and need help with detailed instructions (dummy version for me).
I have a table with various columns and the following columns, specifically:
ReviewerComments
NewComment
I created a form with both of these fields and need to create an Append Comment button that moves the text from NewComment field and appends it to ReviewerComment field and time/date stamps the comments as they are added. I named this button cmdAppendComment.
I had seen someone else post something and I tried, but as I am completely new to this I know I messed it up. Any help is greatly appreciated.
This is what the VBA code looks like right now:
Private Sub cmdAppendComment_Click()
If (IsNull(NewComment.Value)) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(ReviewerComments.Value)) Then
ReviewerComments.Value = NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
ReviewerComments.Value = ReviewerComments.Value & _
vbNewLine & vbNewLine & _
NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
NewComment.Value = ""
End Sub
I have some suggestions for your code:
1. Do not check if a textbox is null but how many characters your textbox has. You should always do it that way because otherwise you tend to get errors.
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
Here you check the length of the string in your textbox. You need to append "" because otherwise you tend to get null-errors or something similar.
2. You forgot to reference the objects in your form correctly. You have your form and in that form you put your textboxes and also your VBA-code. Your can reference all your objects with "Me.[FormObjects]".
The compiler complains that "NewComment.Value" or "ReviewerComment.Value" is not initialized or in other words not dimensioned. With correct reference this should stop.
Private Sub cmdAppendComment_Click()
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(Me.ReviewerComments.Value)) Then
Me.ReviewerComments.Value = Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
Me.ReviewerComments.Value = Me.ReviewerComments.Value & _
vbNewLine & vbNewLine & _
Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
Me.NewComment.Value = ""
End Sub

Putting a hyperlink in a MessageBox

Is it possible to add a hyperlink to a messagebox? I'm trying to do something like this:
MsgBox "Sorry, but you have an out-of-date database version. Please redownload via the batch file to ensure that you have the latest version. Contact the administrator of this database or your manager if you need help." & vbCr _
& vbCr _
& "Your current database version is " & CurrentVer & " which may be out of date. The current database version prescribed on the network share is " & FileVer & ". They must match in order for you to proceed." & vbCr _
& vbCr _
& "For CSC self-help instructions on how to reload the most current version of the PRF Intake Tool to your computer, please click the link below to be directed to CSC Online instructions." & vbCr _
& vbCr _
& "http://www.OurSite.com/online/Solutions/Search_Results.asp?opsystem=7&keywords=PRF+Intake+Tool&Category=", , "There is a problem..."
The problem is, the hyperlink isn't clickable. I'd like to do this so that the users can just click the link and have the download begin automatically.
I'm using Access 2010 in a Win7 environment.
A straight-forward answer is NO. MsgBox does not allow hyperlinks, just plain text.
Thus, here is a workaround that should work alright.
Set objShell = CreateObject("Wscript.Shell")
intMessage = MsgBox("Sorry, but you have an out-of-date database version. Please redownload via the batch file to ensure that you have the latest version. Contact the administrator of this database or your manager if you need help." & vbCr _
& vbCr _
& "Your current database version is " & CurrentVer & " which may be out of date. The current database version prescribed on the network share is " & FileVer & ". They must match in order for you to proceed." & vbCr _
& vbCr _
& "Would you like to learn more about CSC self-help instructions on how to reload the most current version of the PRF Intake Tool to your computer?", _
vbYesNo, "There is a problem...")
If intMessage = vbYes Then
objShell.Run ("http://www.OurSite.com/online/Solutions/Search_Results.asp?opsystem=7&keywords=PRF+Intake+Tool&Category=")
Else
Wscript.Quit
End If
If underline style is a requirement, then you should just create your own User Form as described at http://j-walk.com/ss/excel/tips/tip71.htm. Here are the steps:
Add a Label object and type your message
Make the Label blue (ForeColor) and underlined (Font) so it looks like a typical hyperlink.
Set the Label's MousePointer property to 99 - fmMousePointerCustom
Specify the cursor file for the Label's MouseIcon image (if you have one).
Double-click the Label and create a subroutine the Click event. Here's a sample code:
Private Sub Label1_Click()
Link = "http://www.YOUR_SITE.com"
On Error GoTo NoCanDo
ActiveWorkbook.FollowHyperlink Address:=Link, NewWindow:=True
Unload Me
Exit Sub
NoCanDo:
MsgBox "Cannot open " & Link End Sub
To create a "mail to" hyperlink, use a statement like this:
Link = "mailto:someone#somewhere.com"

Applying vbCrLf to the content of a textbox

I've got a userform within an Excel vba project. At design-time it is empty.
In the form initialize event I've got the following code:
Private Sub UserForm_Initialize()
txtSQL.value = _
"SELECT MyName = ""ColY"" " & vbCrLf & _
"FROM SomeTable " & vbCrLf & _
"GROUP BY Customer " & vbCrLf & _
"ORDER BY Customer DESC"
End Sub
I was hoping for 4 separate lines of text in the textbox but instead have the following:
Am I using the wrong control? Or am I using the right control in the wrong way?
Whoops: maybe I should go to bed...
At design-time set multi-line property to True!