MS Project VBA getting or setting Title of custom task field Text1..Text30 - vba

I try to change the title of the task fields Text1 to Text30 in Microsoft Project 2007 using VBA.
Here is what I do manually:
In Gant Chart Task Table click on table header and add a column. In the popup I can select which task property to add, in my case “Text1” and I can enter a title, for example “my text1”.
But I don’t care about the table. I want to give the title to the text field.
I want to export Text1 to Text30 to an XML file and like to export the title of the field as well, so I need to get the title and I like to set it, because even if it isn’t used in a table it shallbe exported.
Here is what I wrote just for testing:
Private Sub setfieldtitletryout()
Dim i As Integer
Dim c As Long
For i = 1 To 30
c = FieldNameToFieldConstant("Text" & i, pjTask)
Debug.Print "Text" & i; " has constant " & c
Debug.Print " Name of Text" & i; " is " & FieldConstantToFieldName(c) ' well what a surprise...
SetFieldTitle(c, ListOfNames(i)) ' Oviously doesn't work, because the function doesn't exist :-(
Debug.Print " Title of Text" & i; " is " & FieldConstantToFieldTitle(c) ' unfortunately doen't exist too
Next
End Sub
Here is what I checked but did not ready help me…
http://msdn.microsoft.com/en-us/library/bb221504(office.12).aspx
http://msdn.microsoft.com/en-us/library/bb221254(office.12).aspx
I’d be glad to get this fixed!
Thanks in advance for helping out!
Cheers
B

Well I did it :-)
Private Sub setfieldtitletryout()
Dim i As Integer
Dim c As Long
For i = 1 To 5
c = FieldNameToFieldConstant("Text" & i, pjTask) ' get constant of custom field by name
Debug.Print i & ". Rename title of Text" & i
Debug.Print " Name of Text" & i; " is '" & FieldConstantToFieldName(c) & "'"
CustomFieldRename FieldID:=c, NewName:="Titel of Text " & i 'Rename/set custom field title
Debug.Print " Title of Text" & i; " is '" & CustomFieldGetName(c) & "'" ' get title of custom field
Next
End Sub
http://msdn.microsoft.com/en-us/library/ms453877(v=office.12).aspx
Help on CustomFieldRename
Help on CustomFieldGetName

Related

Changing text in a contentcontrol is very slow

I have a big table in ms-word that contains 85 contentcontrols (combo boxes). I want to change the content using a vba loop (see below). It takes longer than one minute for it to complete...
Are there other options?
Private Sub Btn_Clear1_Click()
Dim a
Dim c As ContentControl
a = FindTable(ActiveDocument.Name, "myTableName")(1) 'returns an array(Long) with number of table found
For Each c In ActiveDocument.Tables(a).Range.ContentControls
c.Range.text = "MY CHANGED TEXT"
Next c
End Sub
Thanks in advance for any hint!
Here, turning off screenupdating reduces the time from about 6 seconds to less than 1 second. e.g.
On Error Goto turnscreenon
Application.Screenupdating = False
For Each c In ActiveDocument.Tables(a).Range.ContentControls
c.Range.text = "MY CHANGED TEXT"
Next c
turnscreenon:
Application.Screenupdating = True
That may only work on the Windows version of Word.
If you know exactly how many combo boxes there are going to be, you could consider creating a custom xml part containing an array of XML Elements to contain the values. Map each content control to one of those elements. Then instead of writing the values to the content control ranges, write them to the XML Part and let Word do the work. That works almost instantaneously here.
e.g. in a simple scenario where you just have those 85 content controls in the table, you could set up the Custom XML Part like this (I leave you to write any code that you need to delete old versions). You should only need to run this once.
Sub createCxpAndLink()
' You should choose your own Uri
Const myNamespaceUri As String = "mycbcs"
Dim a
Dim i As Long
Dim s As String
Dim cxp As Office.CustomXMLPart
With ActiveDocument
a = FindTable(.Name, "myTableName")(1)
s = ""
s = s & "<?xml version='1.0' encoding='UTF-8'?>" & vbCrLf
s = s & "<cbcs xmlns='" & myNamespaceUri & "'>" & vbCrLf
For i = 1 To .Tables(a).Range.ContentControls.Count
s = s & " <cbc/>" & vbCrLf
Next
s = s & "</cbcs>"
Set cxp = .CustomXMLParts.Add(s)
With .Tables(a).Range.ContentControls
For i = 1 To .Count
.Item(i).XMLMapping.SetMapping "/x:cbcs[1]/x:cbc[" & Trim(CStr(i)) & "]", "xmlns:x='" & myNamespaceUri & "'", cxp
Next
End With
Set cxp = Nothing
End With
End Sub
Then to update the contents you need something like this
Sub testsetxml()
Const myNamespaceUri As String = "mycbcs"
Dim i As Long
'our start time...
Debug.Print Now
With ActiveDocument.CustomXMLParts.SelectByNamespace(myNamespaceUri)(1)
For i = 1 To 85
.SelectNodes("/ns0:cbcs[1]/ns0:cbc[" & Trim(CStr(i)) & "]")(1).Text = "my changed text "
' or if you want to put different texts in different controls, you can test using e.g.
.SelectNodes("/ns0:cbcs[1]/ns0:cbc[" & Trim(CStr(i)) & "]")(1).Text = "my changed text " & Cstr(i)
Next
End With
'our end time...
Debug.Print Now
End Sub
(NB you cannot do it by mapping all the controls to a single XML element because then all the dropdowns will all be updated to the same value whenever you change the value of one of them.)
Apologies for any typos - I've changed the code to be more in line with what you have already and have not tested the changes.

How to add a comment using comment boxes in Access?

I am building an Access database for work. I have set up a report to open upon click for a certain record. So only the information of that record is to appear on the report. However, I would like to add a comment box in the report where you can add comments. The new comments are stamped and added to the previous comments already showing in the report. I was able to program the commenting function in a separate report. However, for the reports that show only specific records it won't work. I know it is because I have to somehow add each comment to my database, but I just can not figure out how to do it. I used the following code that I found online in another article. It works fine when your comments are not tied to a certain record.
Private Sub cmdAppendComment_Click()
If (IsNull(txtNewComment.Value)) Then
MsgBox ("Please enter a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
' These commented lines will never be reached:
' If (IsNull(txtComment.Value)) Then
' Table.tblmain.User_comment.Value = txtNewComment.Value & " ~ " & _
' VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
' Else
Table.tblmain.User_comment.Value = txtComment.Value & _
vbNewLine & vbNewLine & _
txtNewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
' End If
' txtNewComment.Value = ""
' Use Null:
txtNewComment.Value = Null
End Sub
You would use a form, not report, for this.
Bind this to the table, add the new comment to the existing comment bound to texbox, and save the record.

Run-time error: The value you entered isn't valid for this field

I am trying to update a record using Subform. When I update the first time it updates properly but when I try to update the same record again I am getting the error:
Run-time error '-2147352567 (80020009)': The value you entered isn't valid for this field
The following is the form.
When I click edit, the information from the selected record is populated into the respective text-boxes. Once I update the information and click update, the record gets successfully updated for the first time.
When I try to update the same record again I get the mentioned error.
Here is the VB script that runs on clicking edit.
Private Sub cmdEdit_Click()
'Check if data exists in the list
If Not (Me.frmschoolsub.Form.Recordset.EOF And Me.frmschoolsub.Form.Recordset.BOF) Then
'get data to text box control
With Me.frmschoolsub.Form.Recordset
Me.Schooltxt = .Fields("School_Name")
Me.Desctxt = .Fields("Description")
Me.Deantxt = .Fields("Dean")
Me.Adeantxt = .Fields("Associate_Dean")
'store id of student in tag
Me.Schooltxt.Tag = .Fields("School_ID")
'change caption of button to update
Me.cmdAdd.Caption = "Update"
Me.cmdEdit.Enabled = False
End With
End If
End Sub
When I click on Debug it highlights the following line.
Me.Schooltxt = .Fields("School_Name")
Can you help me in identifying what is the issue here.
I figured that after the each update, I am losing the position of record. I added the following statement after update and Requery
Me.frmschoolsub.Form.Recordset.MoveFirst
Following is the code snippet.
Else
CurrentDb.Execute "Update School " & _
" SET School_Name ='" & Me.Schooltxt & "'" & _
", Description ='" & Me.Desctxt & "'" & _
", Dean ='" & Me.Deantxt & "'" & _
", Associate_Dean='" & Me.Adeantxt & "'" & _
"where School_ID=" & Me.Schooltxt.Tag
End If
'Clear the Fields
cmdClr_Click
'Refresh the table
frmschoolsub.Form.Requery
Me.frmschoolsub.Form.Recordset.MoveFirst
This fixed the issue.
This error is happening that a form field cannot be referenced to a textbox like you did.
You can do it as below.
With Me.frmschoolsub.Form.Recordset
Me.Schooltxt = Forms![<<if you have main form >>]![frmschoolsub].form![School_Name]

MS access sum results between two dates as text box in form

I issue following query in SQL and works fine
SELECT SUM(goudOpkoop.winst)
FROM goudOpkoop
WHERE goudOpkoop.date
BETWEEN '2015-1-10' AND '2015-1-22'
I would like to have the results in a Form in Textbox 3 (name Text183) when last of two dates, one in Textbox 1 (name Text179) and other in textbox 2 (name Text181) have been picked.
I think I would need to use AfterUpdate code builder for Textbox 2 and issue there the query to eventually show results in Textbox 3.
I have already linked with SQL server.
Information: ODBC;DSN=Essence Test;;TABLE=goudOpkoop
In me not being a professional in VBA I have no clue how to get this working.
Not tested, but should do the trick slightly, unless I made a typo.
Change the format of your 2 dates textboxes in "Short Date", that way you will have a calendar picker and you will ensure that your date have a correct format
Create this sub in your form module :
Private Sub CheckSUM()
Dim RST As Recordset
Dim SQL As String
' Reset the result textbox
Text183.value = ""
' If your 2 date textboxes are not populated, cancel
If Text179.Value = "" or Text181.Value = "" Then Exit Sub
' Prepare the query, with proper formating of the dates
SQL = " SELECT SUM(goudOpkoop.winst) AS mySum " & _
" FROM goudOpkoop " & _
" WHERE goudOpkoop.Date " & _
" BETWEEN '" & Format(Text179.Value, "YYYY-MM-DD") & "' " & _
" AND '" & Format(text181.Value, "YYYY-MM-DD") & "'"
' Execute the query
Set RST = CurrentDb.OpenRecordset(SQL)
' If the query is valid and returned something, we recuperate the value
If Not RST.BOF Then
Text183.Value = RST!mySum
End If
' Cleaning
RST.Close
Set RST = Nothing
End Sub
Then, for your 2 dates textboxes, create an afterupdate event and call the previous sub in them:
Private Sub Text179_AfterUpdate()
CheckSUM
End Sub
Private Sub Text181_AfterUpdate()
CheckSUM
End Sub

#Name? on form after requery in Access 2010

I am using VBA and SQL to re-query my main form based on criteria entered in several controls on a pop up form. As far as I can tell the code is running correctly, the database is re-queried based on the criteria I enter, but 2 of my controls on my main form show as #Name? or blank after re-querying based on the criteria. Anyone know how I can fix this???
The code that runs the re-query is:
Public Sub SuperFilter()
On Error GoTo Err_AdvancedFilter_Click
Dim strSQL As String
Dim strCallNumber As String
Dim strAsgnTech As String
Dim strClientID As String
Dim strCallGroup As String
Dim strPriority As String
Dim strOpenStatus As String
If IsNull(Forms![frmTips&Tricks].txtCallNumber) = False Then
strCallNumber = " (((CallInfo.CallNumber) = forms![frmTips&Tricks].[txtCallNumber])) and "
Else
strCallNumber = ""
End If
If IsNull(Forms![frmTips&Tricks].cboAsgnTech) = False Then
strAsgnTech = " (((CallInfo.AsgnTech) = forms![frmTips&Tricks].[cboasgntech])) and "
Else
strAsgnTech = ""
End If
If IsNull(Forms![frmTips&Tricks].cboClientID) = False Then
strClientID = " (((CallInfo.ClientID) = forms![frmTips&Tricks].[cboClientID])) and "
Else
strClientID = ""
End If
If IsNull(Forms![frmTips&Tricks].cboCallGroup) = False Then
strCallGroup = " (((CallInfo.AsgnGroup) = forms![frmTips&Tricks].[cboCallGroup])) and "
Else
strCallGroup = ""
End If
If IsNull(Forms![frmTips&Tricks].cboPriority) = False Then
strPriority = " (((CallInfo.Severity) = forms![frmTips&Tricks].[cboPriority])) and "
Else
strPriority = ""
End If
If Forms![frmTips&Tricks].optOpenStatus.Value = 1 Then
strOpenStatus = " (((CallInfo.OpenStatus) = True))"
Else
strOpenStatus = " (((CallInfo.OpenStatus) is not null ))"
End If
strSQL = "SELECT CallInfo.CallNumber, CallInfo.ClientID,* " & _
"FROM dbo_HDTechs INNER JOIN ([User] INNER JOIN CallInfo ON User.ClientID = CallInfo.ClientID) ON dbo_HDTechs.TechName = CallInfo.AsgnTech " & _
"WHERE " & strCallNumber & strAsgnTech & strClientID & strCallGroup & strPriority & strOpenStatus & _
"ORDER BY CallInfo.RcvdDate;"
Form.RecordSource = strSQL
Me.cboCallNumber.RowSource = strSQL
Form.Requery
If Me.RecordsetClone.RecordCount = 0 Then
MsgBox "No Records Found: Try Diferent Criteria."
Form.RecordSource = "qryservicerequestentry"
Me.cboCallNumber.RowSource = "qryservicerequestentry"
Exit Sub
End If
Me.cmdSuperFilterOff.Visible = True
Exit Sub
Exit_cmdAdvancedFilter_Click:
Exit Sub
Err_AdvancedFilter_Click:
MsgBox Err.Description
Resume Exit_cmdAdvancedFilter_Click
End Sub
The first control in question is a combo box that displays the Client Name from the CallInfo form (Main Form).
Control Source: ClientID
And when expanded lists all available clients to select from the Users form (User ID is linked between the User form and CallInfo form).
Row Source: SELECT User.ClientID FROM [User];
After the re-query, this combobox will be blank, sometimes showing #Name? if you click on it.
The second control in question is a text box that shows the Client's phone number.
Control Source: PhoneNo
After the Re-query, this text box always displays #Name?
The third control in question is a text box that displays the clients office location.
Control Source: Location
What really baffles me is that THIS text box displays correctly after the re-query. I don't know why it would display the correct data when the Phone Number text box does not, seeing as they are so similar and work with similar data....
To Compare, the The form record source is normally based on:
SELECT CallInfo.CallNumber, CallInfo.ClientID, CallInfo.RcvdTech, CallInfo.RcvdDate, CallInfo.CloseDate, CallInfo.Classroom, CallInfo.Problem, CallInfo.CurrentStatus, CallInfo.Resolution, CallInfo.Severity, CallInfo.OpenStatus, CallInfo.AsgnTech, dbo_HDTechs.Email, CallInfo.FullName, CallInfo.AsgnGroup, User.Location, User.PhoneNo, CallInfo.OpenStatus
FROM dbo_HDTechs INNER JOIN ([User] INNER JOIN CallInfo ON User.ClientID = CallInfo.ClientID) ON dbo_HDTechs.TechName = CallInfo.AsgnTech
WHERE (((CallInfo.OpenStatus)=True))
ORDER BY CallInfo.RcvdDate;
Just going on what you wrote, I may take a slightly different approach (just personal preference).
I would change all of your 'IsNull' tests to also check for 'Empty'. i.e.
If IsNull(Forms![frmTips&Tricks].cboClientID) = False AND ...cliientID <> ""
Just today I had an issue relating to form references in a query WHERE clause, so I changed to:
strClientID = " (((CallInfo.ClientID) = '" & forms![frmTips&Tricks].[cboClientID] & "')) and"
Add a Debug.Print of your generated SQL, then look at it and try to run that SQL manually
Good Luck,
Wayne
Solved by designating the form in the control source like: CallInfo.ClientID
I still don't know why the Client Office displayed Correctly... Anybody have a hint? :)
TE