PDA

View Full Version : Setting value to the value of another?



gregory6106
11-17-2011, 07:00 AM
Hello,

I'm very new to this but really enjoying it. I think if I can get this program to work, I may look into learning a programming language.

Anyway, I'm trying to create a userform in word that does the following:

-Asks you to fill in 2 text boxes
-Checks to see if you did
-If the second text box is left blank intentionally, the program stores the text entered in the first box as both boxes.

Make sense?

Should be very simple, but it's not displaying properly. Here's what I'm using:

If txtReport.Value <> "" Then
txtReport.Value = txt.Incident.Value
End If
.Bookmarks("Incident").Range.Text = txtIncident.Value
.Bookmarks("Report").Range.Text = txtReport.Value

On the document, this displays the first text box entry, "txtIncident.Value"
However it does not display the second text box entry, "txtReport.Value" but rather leaves it blank. If I fill in different values for both text boxes, it displays both of them just fine.

Please help. I've been all over the web for the past two days and can't figure this out. :banghead: Thank you so so so much for your time.

gmaxey
11-17-2011, 07:43 AM
Option Explicit
Private Sub CommandButton1_Click()
FillBM "bmReport", Me.txtReport.Text
If Me.txtIncident.Text <> "" Then
FillBM "bmIncident", Me.txtIncident.Text
Else
FillBM "bmIncident", Me.txtReport.Text
End If
Unload Me
End Sub
Sub FillBM(pName As String, pVal As String)
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks(pName).Range
oRng.Text = pVal
ActiveDocument.Bookmarks.Add pName, oRng
End Sub

gregory6106
11-17-2011, 08:37 AM
Thank you so much for your help. But I have a limited understanding, (trying to learn) and am a bit confused. I'm studying the code you gave me, and from what I can tell it's basically filling the bookmark fields with one variable or the other based on the If / Else statement? Throughout the rest of the main document that will be created from this userform, many of these variables will be reused in other bookmarks. So is this just telling the program to write the incident number in the report number bookmark on this one occasion or to replace the value of report, which is nothing (left blank) with the value of incident number so that any other time the report number is called up it display the incident number.

I'm sure I'm making this much more confusing than it needs to be. So sorry.

Thanks for your time.

gmaxey
11-17-2011, 08:49 AM
It assumes that txtReport will always have a value entered via the UserForm and and txtIncident may or may not.

It always writes the value of txtReport to the bookmark in the document bmReport.

If the form user enteres data in txtIncident then the text entered in written to the bookmakr "bmInicident" if the document. If the form user "does not" enter date in txtIndcident then the value of txtReport is written to the bookmark "bmIncident" in the document.

The txtIncident value is never written to the bmReport bookmark.


You only need one bookmark in the document. For example, lets say we have a UserForm with a text field where we fill in our name:

In the document we would have a bookmark "bmName"



Option Explicit Private Sub CommandButton1_Click()
FillBM "bmName", Me.txtName.Text
Unload Me End Sub
Sub FillBM(pName As String, pVal As String) Dim oRng As Word.Range Set oRng = ActiveDocument.Bookmarks(pName).Range oRng.Text = pVal ActiveDocument.Bookmarks.Add pName, oRng End Sub


The bookmark bmName now defines a range of text (e.g., "Gregory")

If we want our name to appear in ohter locations in the document then we just need to use REF fields:

{REF bmName}

gmaxey
11-17-2011, 08:59 AM
Greg,

You can force the actual content of one textbox to = the value of another:

Private Sub CommandButton1_Click()
If Me.TextBox1.Text = "" Then Exit Sub
If Me.TextBox2.Text = "" Then
Me.TextBox2.Text = Me.TextBox1.Text
End If
MsgBox Me.TextBox1.Text
MsgBox Me.TextBox2.Text
Unload Me
End Sub

gregory6106
11-17-2011, 09:12 AM
Alright, so I thought I was getting the hang of this, but jeeeeez, I'm confused. Maybe it would help if I posted my whole script, which isn't very long. It may give you a better idea of what I'm trying to do and how limited my knowledge is. I understand if my issue would be too time consuming as I know you have other questions to answer and issues to resolve. But here is what I have. If it's evident from seeing this that I'm just too far behind the curve, could you reccomend a good easy to follow reference for me to look at and follow?

Private Sub Document_New()
frmTestProject.Show
txtName.Value = Null
txtDOB.Value = Null
End Sub
Private Sub CmdClear_Click()
txtName.Value = Null
txtDOB.Value = Null
End Sub
Private Sub CmdExit_Click()
Unload Me
ActiveDocument.Close SaveChanges:=False
End Sub
Private Sub CmdSubmit_Click()
Application.ScreenUpdating = False
With ActiveDocument
.Bookmarks("OfficerName").Range.Text = txtOfficerName.Value
.Bookmarks("Badge").Range.Text = txtBadge.Value
.Bookmarks("Assignment").Range.Text = txtAssignment.Value
If ChkBxInvestigatingOfficer = True Then
.Bookmarks("InvestigatingOfficer").Range.Text = txtOfficerName.Value
.Bookmarks("InvestigatingBadge").Range.Text = txtBadge.Value
.Bookmarks("InvestigatingAssignment").Range.Text = txtAssignment.Value
Else
.Bookmarks("InvestigatingOfficer").Range.Text = txtInvestigatingOfficer.Value
.Bookmarks("InvestigatingBadge").Range.Text = txtInvestigatingBadge.Value
.Bookmarks("InvestigatingAssignment").Range.Text = txtInvestigatingAssignment.Value
End If
If ChkBxProcessingOfficer = True Then
.Bookmarks("ProcessingOfficer").Range.Text = txtOfficerName.Value
.Bookmarks("ProcessingBadge").Range.Text = txtBadge.Value
.Bookmarks("ProcessingAssignment").Range.Text = txtAssignment.Value
Else
.Bookmarks("ProcessingOfficer").Range.Text = txtProcessingOfficer.Value
.Bookmarks("ProcessingBadge").Range.Text = txtProcessingBadge.Value
.Bookmarks("ProcessingAssignment").Range.Text = txtProcessingAssignment.Value
End If
If txtReport.Value <> "" Then
txtReport.Value = txt.Incident.Value
End If
.Bookmarks("Incident").Range.Text = txtIncident.Value
.Bookmarks("Report").Range.Text = txtReport.Value
End With
Application.ScreenUpdating = True
Unload Me
End Sub

gregory6106
11-17-2011, 09:13 AM
I do use indents to make it easy to read, they just didn't copy and paste for some reason. Sorry

gmaxey
11-17-2011, 09:47 AM
Greg,

User the "VBA" icon to insert delimiters and post your code between them:

This line of code is a bit confusing:

"If txtReport.Value <> "" Then"

It is saying that if the user enters "anything" in the txtReport field then to use the value in the "txtIncident" field instead. I think you mean that if the user leaves the "txtReport" field blank then use the value entered in the txtIncident field. Is that right?

You can fill in the rest. This code makes a call to the FillBM procedure to process each userform text field.





Private Sub CmdSubmit_Click()
Application.ScreenUpdating = False
FillBM "OfficerName", txtOfficerName.Text
FillBM "Badge", txtBadge.Text
FillBM "Assignment", txtAssignment.Text
'I think you mean if there is no value in txtReport to use the value in txtIncident
If txtReport.Value = "" Then
txtReport.Text = txtIncident.Text
End If
FillBM "Incident", txtIncident.Text
FillBM "Report", txtReport.Text
Application.ScreenUpdating = True
Unload Me
End Sub
Sub FillBM(pName As String, pVal As String)
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks(pName).Range
oRng.Text = pVal
ActiveDocument.Bookmarks.Add pName, oRng
End Sub

Frosty
11-17-2011, 10:12 AM
Just another thought:

When using UserForms, I think that's a good way to present as much as you can what is going to happen when you click the OK button. So rather than just setting the txtReport.Text property to the txtIncident.Text property when you click the OK button, I would say set the txtReport.Text property to the txtIncident.Text on the txtIncident_Change event, if txtReport.Text = "".

What this does is present to the user what is going to happen (as well as provide a default value if they do nothing). Without having the userform, code, I think it would be something like adding the following to your form code.


Private Sub txtIncident_Change()
If txtReport.Value = "" Then
txtReport.Text = txtIncident.Text
End If
End Sub

If you like this change, you could remove that same If...End If block from the CmdSubmit_Click event.

Just another approach, which gives a visual cue to the user what is about to happen before it actually happens.

gregory6106
11-18-2011, 07:32 AM
Thanks so much! It works!