Visual Studio 2012 - Add multiple items on same writeline - vba

Okay, so what i"m doing is creating a word file with my application with my labels and textboxes to create a receipt. In my word file, I am trying to write what is in a first name label beside what is in a last name label.
Here is the code I have, but I just seem to be missing something, because the word file keeps putting a huge gap between the first name and last name. I'm sure it's a very simple fix, but Google doesn't seem to be helping with what I am trying to describe.
printFile.WriteLine("Name: " & vbTab & FirstNameTextBox.Text + LastNameTextBox.Text)

I assume it must be the vbTab keyword.
which is fixed and cannot be changed.
That said you can manage a workaround as follows:
Public Const MyTab = " "
and in your code
printFile.WriteLine("Name: " & MyTab & FirstNameTextBox.Text + LastNameTextBox.Text)

Related

Microsoft Access - Using a form to input data into a different table with VBA?

Trying to use a form named 'Customer Entry', that when clicked, enters the data that has been typed in said form into a table that is named 'CustomerRecord'. I keep getting errors and am at my wits end. Here is my code below, this is in VBA.
Public Sub Command19_Click()
CurrentDb.Execute "INSERT INTO CustomerRecord(Customer Name, APM, UAID, Context Code, Purpose Code, Context Description, Purpose Description) " & _
" VALUES (" & Me.txtCustomer Name & ", " & Me.txtAPM & ", " & Me.txtUAID & ", " & Me.txtContextCode & "," & Me.txtPurposeCode & ", " & Me.txtContext Description & ", " & Me.txtPurpose Description & ")"
frmCustomer
Entry.Form.Requery
End Sub
If you want to edit data from two tables on a single form you need to make an updatable query to base your form upon. Set your forms RecordSource property to be the updatable query. Now you can add form elements from the source that allow the user to edit all the fields directly.
See this list of pitfalls to ensure that your query is updatable:
http://allenbrowne.com/ser-61.html
If you absolutely must edit data elsewhere from your form, which you occasionally must do, don't use an SQL query execute statement to do so. Use a recordset object instead. This is both more secure, more reliable, and easier to read the code.
See this guide for an example of how it's done: https://learn.microsoft.com/en-us/office/vba/access/Concepts/Data-Access-Objects/modify-an-existing-record-in-a-dao-recordset
Additional reading: https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/recordset-edit-method-dao

Saving a combobox id instead of the text

Good morning people, or rather midnight.
I am totally new (again) to programming since I had to drop college for years due a lack of economic power and started fresh over,hence after several years I have forgotten almost everything.
Here is the thing though, I never thought I would have problem with a simple insert into. I am using visual basic and I am trying to save the following sentence in a database
Dim insertQuery As String = "INSERT INTO Camion (Plate, BrandID , ModelID, Year, Engine, Chasis, WeightLimit ,DateofPurchase) VALUES ('" & txtplate.Text & "','" & cmbBrand.Text & "','" & cmbModel.Text & "','" & Year.Value & "','" & txtEngine.Text & "','" & txtChasis.Text & "','" & nudWeightLimit.Value & "','" & dtpDateofPurchase.Value & "')"
ExecuteQuery(insertQuery)
MessageBox.Show("Info succesfully saved")
Now here is the issue. My combo box shows the name of the items instead of the code which is ok, I want it to do this so that people know what they are selecting. However, at the time of saving, this goes into a table where it only saves the ID of the combo box (not where it draws the data but a 2nd table where it stores all this info) but the sentence I am showing tries to save the Text displayed in the combo box in a int type of field in the database.
I am using mysql. So that is basically my issue, when I try to save the combo box of let's say brand , on the display list it shows Nissan and Mercedes. (in the database where it pulls that info, nissan is id 1, and mercedes is id 2). So it shows the name on the list as intended so people knows what they are selecting, but when I try to save it. I end up saving the name in a INT field, which shouldn't happen, is there a way to get the ID from the combobox and save that in the database?
Sorry if the question seems terribly silly as I am sure there is a easy solution but I haven't found one, even with google.

How to update an add-in field code without losing field data (MS Word)?

I have an add-in field in MS Word which also has some hidden data. I want to update the field's code text. However, when I do so and read my hidden data again, they are gone. See this minimal example:
Sub testUpdatingFieldCode()
Dim newField As Field
Set newField = ActiveDocument.Fields.Add(Range:=Selection.Range, Type:=wdFieldAddin)
newField.Code.Text = " ADDIN mycode \* MERGEFORMAT "
newField.Data = "mydata"
Debug.Print "Code: " & newField.Code.Text
Debug.Print "Data: " & newField.Data ' works - prints "Data: mydata"
newField.Code.Text = " ADDIN mycodechanged \* MERGEFORMAT "
Debug.Print "Code: " & newField.Code.Text
Debug.Print "Data: " & newField.Data ' doesn't work - prints "Data: "
End Sub
Is this normal behaviour or a bug? Is there a suggested way to update field codes without losing the data?
I could save the Data to a variable like this but I'm worried about performance (I ultimately need to do this for tens or hundreds fields at a time):
Dim previousData As String: previousData = newField.Data
newField.Code.Text = " ADDIN my_code_changed \* MERGEFORMAT "
newField.Data = previousData
Trying your code, it does insert the field and does change the value.
The problem seems to be with .Data because the field is there in the document.
I join John Korchok's suggestion that you use Document variables instead. They can hold text information and it is not normally accessible to the user. It is not in the text of the document and so not subject to accidental deletion by the user. You can have multiple variables in a document. They can be displayed in the document if you want through a DocVariable field which is a reporting, not an editing, field.
Here is a link to the support document on document variables. Two code examples from that page:
ActiveDocument.Variables.Add Name:="Value1", Value:="1"
MsgBox ActiveDocument.Variables("Value1") + 3
For Each myVar In ActiveDocument.Variables
MsgBox "Name =" & myVar.Name & vbCr & "Value = " & myVar.Value
Next myVar
As you can see, this is a much simpler construction than using the AddIn Field. There are several utilities available that give access to document variables from within the User Interface but nothing in Word out of the box that allows the user to change variables without vba.
[EDIT to address concerns in first comment.]
You can use the Feedback mechanism in Word to complain about the problem with the Addin field. I do not know that it will be fixed because there are such better tools available. More likely is that they will delete or deprecate the field. I know of no way to work around this and get the result you want. Even deleting the field and recreating it does not allow you to get the data.
You can use Document Properties as well as Document Variables. Those are available to the user through the Advanced Properties. In addition, certain properties are available through the Quick Parts menu where a change in the property content control in the document changes the property itself. These are mapped to XML nodes. Like Document Variables, all Document Properties can be displayed through a (reporting) field: DocProperty. My page on Repeating Data shows some uses that can be made using these built-in mapped Document Property Content Controls.

VBA Runtime Error 13 because of wrong Format

I want to add a product number to a collection like this:
colData.Add marke.Value + " " + pn.Value (Range-Objekte)
Every time I start the macro it runs in Runtime Error 13.
If the product number is like 256-78979-0980 everything runs fine.
If the product number is like 8898686 the error occurs.
Writing CStr(pn.Value) doesn't solve the issue.
The only solution I found is to change the format of all PNs to type text.
Then I need to go in the cell and press "Enter" after that a sign appears on the cell stating that the number is recognized as text.
After that the macro works fine for this cell but not for the others.
How can I change my data to make it work with my macro?
Avoid using + for concatenation.
Try repalcing of
colData.Add marke.Value + " " + pn.Value
with
colData.Add marke.Value & " " & pn.Value

Google Calendar API attendees VB

I'm trying to use VB to post to the console the name of an event, the start date, and the displayname or email of the attendees for that event.
But I can't seem to get it to display.
Please help!
This is what I have tried which seems logical but doesn't work :(
Console.WriteLine(calendarEvent1.Summary & ". Start date: " & CalendarEvent1.Start.DateTime.ToString() & ". Assigned to: " & calendarEvent1.Attendees.displayName)
.Attendees returns a list, so to return the name of the first attendee, you would use this line instead.
Console.WriteLine(calendarEvent1.Summary & ". Start date: " & CalendarEvent1.Start.DateTime.ToString() & ". Assigned to: " & calendarEvent1.Attendees(0).DisplayName
If you want to list all the attendees, then you would need to iterate over the list in exactly the same way as you do in VB.Net lists.