Salam Ali,
Let's assume:
- You call it from VBA.
- There is a table named "Projects" and it has field "ProjectName".
You can put this code on particular form module or on standard module.
Here i put this code on standard module.
Public Function addNewProject(strProjectName As String) As Boolean
Dim cSQL As String, cParam As String
Dim tableName As String
Dim db As DAO.Database
Dim cQDf As DAO.QueryDef
tableName = "Projects"
cParam = "PARAMETERS [par_projectName] Text(255); "
cSQL = "INSERT INTO " & tableName & "(ProjectName) " & _
"VALUES (par_projectName);"
On Error GoTo commit_failed
Set db = CurrentDb
Set cQDf = db.CreateQueryDef("", cSQL)
With cQDf
.Parameters("par_projectName") = strProjectName
.Execute dbFailOnError
.Close
End With
addNewProject = True: Exit Function
commit_failed:
addNewProject = False
End Function
I make this function returns boolean value (True/False) just as confirmation if committing transaction into the db is success or fail.
Then you can call this function from any module (form/standard module):
Private Sub cmdSaveInput_Click()
If addNewProject(txtProjectName) = True Then
MsgBox "Data saved."
Else
MsgBox "Saving data failed."
End If
End Sub
or from Immediate window (Ctrl+G):
?addnewproject("Watsan Facility")
Hope it helps.