Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Module To Command

Status
Not open for further replies.

naturalsn

Technical User
Apr 26, 2007
68
GB
Good day..

I currently have a module that imports a MS word Document. I currently Run the import by opening the module and F5 which which then provides me with a popup asking my document name. I was wondering if it is possible to place this as a command on a form.

I did to copy/paste this code to the Click event of a Command Button but no luck, The code starts as follow.

Can someone please advise if i am missing something

Kind Regards
Susan


Code:
Option Compare Database
Option Explicit

Function FixField(vIn, Vout) 'determine the value to use if field is empty
If IsNull(vIn) Or Trim(vIn) = "" Then FixField = Vout Else FixField = vIn

End Function

Sub GetWordData()

Dim appWord As Object
Dim doc As Object
Dim rst As New ADODB.Recordset
Dim rstSkills As New ADODB.Recordset
Dim rstLanguage As New ADODB.Recordset
Dim rstEducation As New ADODB.Recordset
Dim rstWork As New ADODB.Recordset
Dim rstRefs As New ADODB.Recordset
Dim sSQL As String
Dim n As Integer
Dim l As Long

Dim strDocName As String
Dim blnQuitWord As Boolean

On Error GoTo ErrorHandling

strDocName = gcDocPath & "\" & InputBox("Enter The Name Of The Form You Would Like To Import", "Import Personal Data From")

Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)

Dim ID As String
ID = FixField(doc.FormFields("ID").Result, 0)

If ID <= 0 Then
MsgBox "Unable to find user ID in document - cannot import record", vbExclamation

Set rst = Nothing
Set appWord = Nothing 'leave word doc open for user review
Exit Sub
End If

rst.Open "SELECT * FROM tblPersonnelInfo WHERE ID = " & ID, CurrentProject.Connection, adOpenDynamic, adLockOptimistic, adCmdText

If rst.EOF Then
MsgBox "Unable to find user ID in database - cannot import record", vbExclamation

Set rst = Nothing
Set appWord = Nothing 'leave word doc open for user review
Exit Sub
End If

'***************** START OF PERSONNEL INFO UPDATE *******************************
With rst
!Title = doc.FormFields("Title").Result

etc etc
 
The button on the form just needs:

[tt]Call GetWordData[/code]
 
I think it is prudent to highlight the importance or Remou's suggestion here. You want to avoid, at all costs, duplicating code in your projects. If you can store your code in one place and reuse it elsewhere, that is the best route. This way, when you need to make a small change to your procedure, you edit it in one place and all of the other areas in your project that reference/call this code will always run the most current version of your code.

Or...you can update the same piece of code multiple times and open a can of worms.

~Melagan
______
"It's never too late to become what you might have been.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top