VBA Code to Module error

ZKHADI

Member
Local time
Today, 09:15
Joined
Apr 5, 2021
Messages
118
hey buddies....

i had a code of Fill word from access which was a function in which this line .FormFields("StudentName").Result = me.Student_name work properly.
but i need multiple fillword files like character certificate , leaving and more.
this code if i copy multiple time in vba so it clashes together so what i did i copied this code to module and and i will little change in it.
now the word file open when i call the module but its empty because i have no idea how i will mention the target table or form. because
in this row .FormFields("StudentName").Result = me.Student_name Me. is not working because now its out side of the form vba code.
see below image and i will attach code. how i will mention the StudentDetailTable or StudentDetailForm which are same. but how ?

See in picture in green box i highlight the row but its not working.. code also attached
Code:
Option Compare Database

Function CharacterCertificateFiller()
Dim appword As Object
Dim doc As Object
Dim Path As String

On Error Resume Next
Error.Clear
Path = "D:\College Database\Documents\CharacterCertificate.docx"
Set appword = GetObject("word.application")
If Err.Number <> 0 Then
Set appword = New Word.Application
appword.Visible = True
End If

Set doc = appword.Documents.Open(Path, True)
With doc

.FormFields("StudentName").Result = StuentRecord.Forms!Student_name
.FormFields("RegistrationNo").Result = Board_University_Registration_No
.FormFields("FatherName").Result = Father_Name
.FormFields("CurrentFaculty").Result = CurrentFacultySemester
.FormFields("Status").Result = StudentStatus
.FormFields("CellNO").Result = Cell_NO

End With

appword.Visible = True
appword.Activate

Set doc = Nothing
Set appword = Nothing

End Function
 

Attachments

  • kdkdk.png
    kdkdk.png
    116.1 KB · Views: 198
change your function to Accept Form object:

Function CharacterCertificateFiller(Byref frm As Form)
...
...
FormFields("StudentName").Result = frm!Student_name
.FormFields("RegistrationNo").Result = frm!Board_University_Registration_No
.FormFields("FatherName").Result = frm!Father_Name
.FormFields("CurrentFaculty").Result = frm!CurrentFacultySemester
.FormFields("Status").Result = frm!StudentStatus
.FormFields("CellNO").Result = frm!Cell_NO
...
...


change the way you call it:

Call CharacterCertificateFiller(Me)
 
i had a code of Fill word from access which was a function
Code:
''add a line to the beginning of the module
Option Explicit

''and temporarily comment out the line to see compilation errors .
On Error Resume Next
 
only 2 boxes updating but 3 still blank and the code is correct why like this?
 

Attachments

  • Untitled.png
    Untitled.png
    167.2 KB · Views: 176
"where" are you calling the function?
show us a snapshot of your form and which button are you calling it.
 
in first picture the StudentRecord Form the Button on click calling the Module
in 2nd Picture the module and the module code
 

Attachments

  • Untitled.png
    Untitled.png
    552.4 KB · Views: 188
thank you brothers i replace . in place of !... and it worked...
 
you can also use !, if you are going to use the Actual Field name:

StudentRecord![Student Name]
StudentRecord![Father Name]
etc.
 

Users who are viewing this thread

Back
Top Bottom