VBA Code to Module error (1 Viewer)

ZKHADI

Member
Local time
Today, 18:11
Joined
Apr 5, 2021
Messages
86
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: 66

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:11
Joined
May 7, 2009
Messages
16,826
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)
 

SHANEMAC51

Active member
Local time
Today, 16:11
Joined
Jan 28, 2022
Messages
294
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
 

ZKHADI

Member
Local time
Today, 18:11
Joined
Apr 5, 2021
Messages
86
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: 54

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:11
Joined
May 7, 2009
Messages
16,826
"where" are you calling the function?
show us a snapshot of your form and which button are you calling it.
 

ZKHADI

Member
Local time
Today, 18:11
Joined
Apr 5, 2021
Messages
86
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: 55

ZKHADI

Member
Local time
Today, 18:11
Joined
Apr 5, 2021
Messages
86
thank you brothers i replace . in place of !... and it worked...
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:11
Joined
May 7, 2009
Messages
16,826
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

Top Bottom