Read Value from Table (with Condition) and Enter Data into Word - vba

I have a question with my VBA script. So far, my script works fine with entering the data into the word file. The VBA script is connected to a button in my ms access navigation. I want to seperate Male/Female data. To do this, I need to be able to gather the information from the
table => tbl_Customer, Column => Gender, Value/Attribute => Male or Female
After this I want the program to act as mentioned below:
for example: .FormFields("txtMale1").result = [CustomerName]
This is only executed when the gender is male.
If the gender is female another code is executed:
.FormFields("txtFemale1").result = [CustomerName]
How can I implement this. My approach:
If myCustomerType("tbl_Customer", "Male") = True Then
.FormFields("txtMale1").result = [CustomerName]
End If
Does not work.
Function fillwordform3()
Dim appword As Object
Dim doc As Object
Dim Path As String
Dim myCustomerType As String
Dim TenPercent As Double
On Error Resume Next
Error.Clear
Set appword = GetObject(, "word.application")
If Err.Number <> 0 Then
Set appword = CreateObject("Word.Application")
appword.Visible = True
End If
Path = Application.CurrentProject.Path & "\Arbeitszeugnis_Test.docx"
'Path = "C:\Users\tathong\Downloads\Thank you letter.docx"
If FileExists(Path) = False Then
MsgBox "Template File Not Found", vbExclamation, "File Not Found"
Else
Set doc = appword.Documents.Open(Path, , True)
myCustomerType = DLookup("Customer_Type", "tbl_customer_type", "[Customer_Type_ID] = " & Me.Departement_Type_ID)
With doc
.FormFields("txtcustomerName1").result = [CustomerName]
.FormFields("txtDateofBirth").result = [DateofBirth]
.FormFields("txtPlaceofBirth").result = [PlaceofBirth]
.FormFields("txtEntryDate").result = [EntryDate]
.FormFields("txtPosition").result = [Position]
.FormFields("txtBeschaeftigung").result = [Beschaeftigung]
.FormFields("txtBeschaeftigungsg").result = [Beschaeftigungsgrad]
.FormFields("txtCertReceiveDate").result = [Certificate_Receive_Date]
.FormFields("txtOtherCert1").result = [Other_Certificate_Date1]
.FormFields("txtOtherCert2").result = [Other_Certificate_Date2]
.FormFields("txtOtherCert3").result = [Other_Certificate_Date3]
.FormFields("txtOtherCert4").result = [Other_Certificate_Date4]
.FormFields("txtcustomerName2").result = [CustomerName]
.FormFields("txtcustomerName3").result = [CustomerName]
.FormFields("txtcustomerName3a").result = [CustomerName]
.FormFields("txtcustomerName4").result = [CustomerName]
.FormFields("txtcustomerName5").result = [CustomerName]
.FormFields("txtcustomerName6").result = [CustomerName]
.FormFields("txtSuperior").result = [Superior]
.FormFields("txtDepartement").result = [Department]
.FormFields("txtHRResponsible").result = [HR_Responsible]
*If myCustomerType("tbl_Customer", "Male") = True Then
.FormFields("txtMale1").result = [CustomerName]
End If*
appword.Visible = True
appword.Activate
End With
Set doc = Nothing
Set appword = Nothing
End If
End Sub
Private Sub Command317_Click()
Call fillwordform3
End Sub

Related

record is not updating when i use a call function that does the update

I have a an unbound form that with a button that adds new record to a table. It is perfectly working but, now i want to use a sub function (newAddition) that handles the real work and just call it whenever i need but i discover that it is only showing blank fields in the main table when i try to save a new record.
Private Sub Command0_Click()
Dim db As Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("T_MASTER")
Dim Checker As Integer
Dim Duplicate_Checker As Integer
Dim code, prodname, QP1_combo, QP1_name, QP1_CAS, txt_component_Type, txt_CONTENT_Lower_limit, BEARBEITER As String
Dim CONTENT, Informationsquelle, Anzahl_Partner, Anhange, Kommentar, end_datum, datum_kunde, datum_dossier, compedium As Variant
Dim CONTENT_Upper_limit, Bearb_Start_Partner, Bearb_End_Partner, profile As Variant
Dim Date_of_entry, Bearb_Start_Datum, Bearb_End_Datum As Variant
Checker = 0
Duplicate_Checker = 0
'*******************************************************
'Verify that the essential fields have values.
'*******************************************************
If IsNull(Me.txt_code.Value) Then
Checker = MsgBox("Product code cannot be empty", vbOKOnly, "Error")
Me.txt_code.SetFocus
ElseIf IsNull(Me.txt_prodname.Value) Then
Checker = MsgBox("Please enter the product name", vbOKOnly, "Error")
Me.txt_prodname.SetFocus
ElseIf IsNull(Me.txt_QP1_combo.Value) Then
Checker = MsgBox("Please select PURE QP1.", vbOKOnly, "Error")
Me.txt_QP1_combo.SetFocus
ElseIf IsNull(Me.txt_component_Type.Value) Then
Checker = MsgBox("Please select the component type.", vbOKOnly, "Error")
Me.txt_component_Type.SetFocus
ElseIf IsNull(Me.txt_BEARBEITER.Value) Then
Checker = MsgBox("Please fill the bearbeiter field.", vbOKOnly, "Error")
Me.txt_BEARBEITER.SetFocus
End If
'*******************************************************
'Checking for duplicacies in the database.
'*******************************************************
code = Me.txt_code.Value
QP1_combo = Me.txt_QP1_combo.Value
If Checker = 0 Then
Do While Not rs.EOF
If rs("PRODUCT_CODE") = code And rs("PURE_QP1") = QP1_combo Then
Duplicate_Checker = MsgBox("Record already in the database!", vbOKOnly, "Duplicate")
End If
rs.MoveNext
Loop
End If
'*******************************************************
' Reading the values.
'*******************************************************
If Checker = 0 And Duplicate_Checker = 0 Then
prodname = Me.txt_prodname.Value
QP1_name = Me.txt_QP1_name.Value
QP1_CAS = Me.txt_QP1_CAS.Value
Component_Type = Me.txt_component_Type.Value
CONTENT = Me.txt_content.Value
CONTENT_Lower_limit = Me.txt_CONTENT_Lower_limit.Value
CONTENT_Upper_limit = Me.txt_CONTENT_upper_limit.Value
'Date_of_entry = Me.txt_Date_of_entry.Value
BEARBEITER = Me.txt_BEARBEITER.Value
Bearb_Start_Datum = Me.txt_Bearb_Start_Datum.Value
Bearb_Start_Partner = Me.txt_Bearb_Start_Partner.Value
Bearb_End_Datum = Me.txt_Bearb_End_Datum.Value
Bearb_End_Partner = Me.txt_Bearb_End_Partner.Value
Anzahl_Partner = Me.txt_Anzahl_Partner.Value
Informationsquelle = Me.txt_Informationsquelle.Value
Anhange = Me.txt_Anhange.Value
Kommentar = Me.txt_Kommentar.Value
datum_kunde = Me.txt_datum_kunde.Value
datum_dossier = Me.txt_datum_dossier.Value
profile = Me.txt_profile.Value
compedium = Me.txt_compedium.Value
'*******************************************************
'Updating the database.
'*******************************************************
NewAddition
MsgBox ("Record successfully saved")
End If
End Sub`
And this is the sub function
Sub NewAddition()
Dim db As Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("T_MASTER")
rs.AddNew
rs("PRODUCT_CODE") = code
rs("PRODUCT_NAME") = prodname
rs("PURE_QP1").Value = QP1_combo
rs("PURE_NAME_QP1").Value = QP1_name
rs("PURE_CAS_NR").Value = QP1_CAS
rs("Component_Type").Value = Component_Type
rs("CONTENT").Value = CONTENT
rs("CONTENT_lower limit").Value = CONTENT_Lower_limit
rs("CONTENT_upper limit").Value = CONTENT_Upper_limit
rs("Date_of_entry").Value = Date
rs("BEARBEITER").Value = BEARBEITER
rs("Bearb_Start_Datum").Value = Bearb_Start_Datum
rs("Bearb_Start_Partner").Value = Bearb_Start_Partner
rs("Bearb_End_Datum").Value = Bearb_End_Datum
rs("Bearb_End_Partner").Value = Bearb_End_Partner
rs("Anzahl_Partner").Value = Anzahl_Partner
rs("Informationsquelle").Value = Informationsquelle
rs("Anhänge").Value = Anhange
rs("Kommentar").Value = Kommentar
rs("Datum_Statement_Kunde").Value = datum_kunde
rs("Datum_Statement_Dossier").Value = datum_dossier
rs("Profile_Y_N").Value = profile
rs("Compendium_Y_N").Value = compedium
' rs("Thema").Value = topic
rs.Update
End Sub
If i click the button, it brings the prompt, record successfully saved as in the code but doesn't write anyting there. It only creates blank records.
Variables are declared and set locally - they only exist for procedure they are declared in. They are killed when procedure ends. Need to declare variables in module header or use some other method to pass data to other procedure.
Option Compare Database
Option Explicit
Dim code, prodname, QP1_combo, QP1_name, QP1_CAS, txt_component_Type, txt_CONTENT_Lower_limit, BEARBEITER As String
Dim CONTENT, Informationsquelle, Anzahl_Partner, Anhange, Kommentar, end_datum, datum_kunde, datum_dossier, compedium As Variant
Dim CONTENT_Upper_limit, Bearb_Start_Partner, Bearb_End_Partner, profile As Variant
Dim Date_of_entry, Bearb_Start_Datum, Bearb_End_Datum As Variant
__________________________________________________________________________________
Private Sub Command0_Click()
...
VBA requires every variable type to be explicitly declared or it will default to Variant. So on line where you have BEARBEITER As String, only BEARBEITER is a string type, others on that line are Variant. They will work regardless.

Getting Error "ActiveX component can't create object while connecting with CMS supervisor

Hi This is the exact function i am using while pulling the report from CMS R17. Error line is quoted in * Set cvsConn = New cvsConnection * stars.
Function CMSConn(sUserID As String, sPassword As String, sServerIP As String)
Dim cvsApp As cvsApplication
Dim cvsSrv As cvsServer
Dim cvsConn As cvsConnection
Dim iServer As Integer
Dim bConnected As Boolean
bConnected = False
Set cvsApp = New cvsApplication
Set cvsSrv = New cvsServer
***Set cvsConn = New cvsConnection***
'Checks to see if already connected to server
For iServer = 1 To cvsApp.Servers.Count
Set cvsSrv = cvsApp.Servers(iServer)
If cvsSrv.ServerKey Like "*\" & sServerIP & "\*\*\*" Then
bConnected = True
'MsgBox "Avaya Connected! Click OK to proceed."
Exit For
End If
Next iServer
'Initiates connection if one not already established
If bConnected = False Then
If cvsApp.CreateServer(sUserID, sPassword, "", sServerIP, False, "ENU",
cvsSrv, cvsConn) Then
If cvsConn.Login(sUserID, sPassword, sServerIP, "ENU") Then
End If
End If
End If
'Executes CMS report
Dim cvsRepInfo As Object
Dim cvsRepProp As Object
Dim cvsLog As Object
Dim b As Boolean
On Error Resume Next
cvsSrv.Reports.ACD = 1
Set cvsRepInfo = cvsSrv.Reports.Reports("Historical\Designer\Skill Interval SvcLvl")
If cvsRepInfo Is Nothing Then
If cvsSrv.Interactive Then
MsgBox "The report was not found on ACD.", vbCritical Or vbOKOnly,
"Avaya CMS Supervisor"
Else
Set cvsLog = CreateObject("ACSERR.cvsLog")
cvsLog.AutoLogWrite "The report was not found on ACD."
Set cvsLog = Nothing
End If
Else
b = cvsSrv.Reports.CreateReport(cvsRepInfo, cvsRepProp)
If b Then
Application.DisplayAlerts = False
cvsRepProp.Window.Top = 40
cvsRepProp.Window.Left = 40
cvsRepProp.Window.Width = 40
cvsRepProp.Window.Height = 40
cvsRepProp.SetProperty "Splits/Skills",
ThisWorkbook.Sheets("DAILY").Range("x7").Value
cvsRepProp.SetProperty "Date",
ThisWorkbook.Sheets("DAILY").Range("x8").Value
cvsRepProp.SetProperty "Times",
ThisWorkbook.Sheets("DAILY").Range("x9").Value
b = cvsRepProp.ExportData("", 9, 0, False, True, True)
'Closes report
If bConnected = True Then
cvsRepProp.Quit
Else
If Not cvsSrv.Interactive Then cvsSrv.ActiveTasks.Remove
cvsRepProp.TaskID
End If
Set cvsRepProp = Nothing
End If
End If
'Terminates server instance and connection
Set cvsRepInfo = Nothing
If Not cvsSrv.Interactive Then cvsApp.Servers.Remove cvsSrv.ServerKey
If bConnected = False Then
cvsConn.Logout
cvsConn.Disconnect
cvsSrv.Connected = False
End If
Set cvsConn = Nothing
Set cvsSrv = Nothing
Set cvsApp = Nothing
End Function
End Sub

Error on event click + Getting data from database

I'm trying to run an event, but when I run it, I get the error:
The expression On Click you entered as the event property setting
produced the following error: The Expression you entered has a
function containing the wrong number of arguments.
The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure].
There may have been an error evaluating the function, even, or macro.
I'd use the following code:
Public Function CH05_Generate(Sagsnr As String)
Dim WordApp As Word.Application
Dim Doc As Word.Document
Dim WordPath As String
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String
Set db = CurrentDb
sql = "SELECT * FROM Projektdata WHERE Sagsnr Like '" & Sagsnr & "'"
Set rst = db.OpenRecordset(sql, dbOpenDynaset)
WordPath = "My path (Can't show this"
Set WordApp = CreateObject("Word.Application")
Set Doc = WordApp.Documents.Add(WordPath)
With Doc
.FormFields("PName").Result = rst![Projektnavn]
.FormFields("text").Result = Forms![TD-E-PM200-CH05]!Kommentar
.FormFields("S3").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q1
.FormFields("S4").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q2
.FormFields("S5").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q3
.FormFields("S6").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q4
.FormFields("S7").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q5
.FormFields("S8").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q6
.FormFields("S9").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q7
.FormFields("S10").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q8
.FormFields("S11").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q9
.FormFields("S12").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q10
.FormFields("S13").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q11
.FormFields("S14").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q12
.FormFields("S15").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q13
.FormFields("S16").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q14
.FormFields("S17").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q15
.FormFields("S18").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q16
.FormFields("S19").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q17
.FormFields("S20").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q18
.FormFields("S21").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q19
.FormFields("S22").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q20
.FormFields("S23").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q21
.FormFields("S24").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q22
.FormFields("S25").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q23
.FormFields("S26").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q24
.FormFields("S27").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q25
.FormFields("S28").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q26
.FormFields("S29").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q27
.FormFields("S30").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q28
.FormFields("S31").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q29
.FormFields("S32").Result = Forms![TD-E-PM200-CH05]!sub.Form!Q30
.FormFields("S33").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q31
.FormFields("S34").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q32
.FormFields("S35").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q33
.FormFields("S36").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q34
.FormFields("S37").CheckBox.Value = Forms![TD-E-PM200-CH05]!sub.Form!Q35
End With
WordApp.visible = True
WordApp.Activate
WordApp.ActiveDocument.Protect wdAllowOnlyFormFields, True
End Function
What I want to achieve to get data from the "Projektdata" database, and get the correct data, "Projektnavn", to fill out:
.FormFields("PName").Result = rst![Projektnavn]
My database structure is like this:
"SELECT *
FROM dbo.Projektdata p
JOIN dbo.Items i ON p.Sagsnr = i.Sagsnr
WHERE ItemID =" & ItemID & " AND p.Sagsnr Like '" & Sagsnr & "'" -- change "," to AND
I call my function like this: =CH05_Generate()
Public Function CH05_Generate(Sagsnr As String, ItemID As String)
This cannot work - the function expects two parameters, you need to pass them in the function call.

How to make field always valid and read only using VB script

I have a case in which I have field name as Order date for which I need to make it as always valid and it should be valid always.
Below is the script I tried.
Public Function getFolderDetails_FromPath_ForEmailInvoice(ByVal pXDoc As CASCADELib.CscXDocument) As String
On Error GoTo PROC_ERR
Const cProcName = "getFolderDetails_FromPath_ForEmailInvoice"
DbgOut(cProcName, Err, "Start")
Dim FullPath As String
Dim PathArry() As String
Dim FolderName As String
Dim FolderProp() As String
Dim xfolder As CscXFolder
Set xfolder = pXDoc.ParentFolder
While Not xfolder.IsRootFolder
Set xfolder = xfolder.ParentFolder
Wend
'Added below line for KTM testing
'FullPath="F:\EmailImport\chirag#gmail.com_08-01-2014_00-00-00\Demo_Manipulados_02.TIF"
If xfolder.XValues.ItemExists("AC_FIELD_OriginalFileName") Then
FullPath= xfolder.XValues.ItemByName("AC_FIELD_OriginalFileName").Value
End If
PathArry() = Split(FullPath,"\")
FolderName = PathArry(UBound(PathArry())-1)
FolderProp() = Split(FolderName,"_")
If CInt(UBound(FolderProp()))=2 Then
If(ValidateEmailAddress(FolderProp(0))) Then
pXDoc.Fields.ItemByName("Email_ID").Text = FolderProp(0)
pXDoc.Fields.ItemByName("Email_ID").ForcedValid = True
pXDoc.Fields.ItemByName("ReceiveDate").Text = FolderProp(1)
ValidationForm.Fields.ItemByName("ReceiveDate").Enabled = False
pXDoc.Fields.ItemByName("ReceiveDate").OriginalValid = True
pXDoc.Fields.ItemByName("Email_Time").Text = FolderProp(2)
pXDoc.Fields.ItemByName("Email_Time").ForcedValid = True
Else
pXDoc.Fields.ItemByName("Email_ID").Text = ""
pXDoc.Fields.ItemByName("Email_ID").ForcedValid = True
pXDoc.Fields.ItemByName("Email_Time").Text = ""
pXDoc.Fields.ItemByName("Email_Time").ForcedValid = True
End If
Else
pXDoc.Fields.ItemByName("Email_ID").Text = ""
pXDoc.Fields.ItemByName("Email_ID").ForcedValid = True
pXDoc.Fields.ItemByName("Email_Time").Text = ""
pXDoc.Fields.ItemByName("Email_Time").ForcedValid = True
End If
Use this property to make field valid always true:
pXDoc.Fields.ItemByName("Email_Time").ExtractionConfident = True

How to search a record using find next method

I have multiple records with the same customer number and I using Find next method to search for the next record with the customer number is same. my code will only search for the 2nd record and not go for the 3rd or 4th search for the same customer number. below is the code can you please help
Private Sub Command114_Click()
Dim db As dao.Database
Dim rs1 As dao.Recordset
Dim pn As Long
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("Application", dbOpenDynaset)
If (Text85 & vbNullString) = vbNullString Then
MsgBox "Please enter the Account no/CIF"
Else
pn = Me.Text85.Value
rs1.FindNext "[Cus_Number] = " & pn
If rs1.NoMatch Then
MsgBox ("Sorry The Accountno/CIF is not found")
Else
Me.S_No = rs1.Fields("sno").Value
Me.Cus_Name = rs1.Fields("Cus_Name").Value
Me.App_level1 = rs1.Fields("App_level1").Value
Me.App_level2 = rs1.Fields("App_level2").Value
Me.App_level3 = rs1.Fields("App_level3").Value
Me.Dec_level1 = rs1.Fields("Dec_level1").Value
Me.Dec_level2 = rs1.Fields("Dec_level2").Value
Me.Dec_level3 = rs1.Fields("Dec_level3").Value
Me.Com_level1 = rs1.Fields("Com_level1").Value
Me.Com_level2 = rs1.Fields("Com_level2").Value
Me.Com_level3 = rs1.Fields("Com_level3").Value
Me.Date1 = rs1.Fields("Date1").Value
Me.Date2 = rs1.Fields("Date2").Value
Me.Date3 = rs1.Fields("Date3").Value
End If
End If
rs1.FindNext "[Cus_Number] = " & pn
Set rs1 = Nothing
End Sub
I am assuming the functionality you want is to change all instances (2, 3, 4 etc.) to the values entered. Remove this (the one near the end, after the End If):
rs1.FindNext "[Cus_Number] = " & pn
and put the FindNext into a loop which will keep on finding and updating your records until there is NoMatch:
rs1.FindNext "[Cus_Number] = " & pn
If rs1.NoMatch Then
MsgBox ("Sorry The Accountno/CIF is not found")
Else
Do Until rs1.NoMatch
Me.S_No = rs1.Fields("sno").Value
Me.Cus_Name = rs1.Fields("Cus_Name").Value
Me.App_level1 = rs1.Fields("App_level1").Value
Me.App_level2 = rs1.Fields("App_level2").Value
Me.App_level3 = rs1.Fields("App_level3").Value
Me.Dec_level1 = rs1.Fields("Dec_level1").Value
Me.Dec_level2 = rs1.Fields("Dec_level2").Value
Me.Dec_level3 = rs1.Fields("Dec_level3").Value
Me.Com_level1 = rs1.Fields("Com_level1").Value
Me.Com_level2 = rs1.Fields("Com_level2").Value
Me.Com_level3 = rs1.Fields("Com_level3").Value
Me.Date1 = rs1.Fields("Date1").Value
Me.Date2 = rs1.Fields("Date2").Value
Me.Date3 = rs1.Fields("Date3").Value
rs1.FindNext "[Cus_Number] = " & pn
Loop
End If
In general, though, I'm not sure what you are looking to do. Are you looking to update the recordset with the latest information on the form? The code you have will overwrite the current values on the form with the last set of found values in the recordset. I would have thought you want the opposite...