Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / January 2008

Tip: Looking for answers? Try searching our database.

programatically insert record

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jonathan Brown - 30 Jan 2008 21:25 GMT
I have a many to many relationship between security clearances and billets.  
a clearance can be associated with more than one billet and a single billet
can be associated with many clearances.  That; however, may not have anything
to do with my problem.

I've built a form that has a listbox that displays the assocaited billets
for an employee's clearance.  Next to it I put a drop down list box that
looks up the list of billets and stores it's value for later use.  I've then
added an "add" button underneath the drop down list box.  What i'd like to do
is choose a given billet from my drop down list box and then click add.  It
should create a new record in my junction table with the value of the drop
down list box in the billet field.

I can't figure out the code behind my add button.  I'm sure the sql
statement should be something like:

"INSERT INTO tblbilletjoin (clearancenum, billetnum) SELECT
tblBilletJoin.ClearanceNum, tblBilletsjoin.BilletNum From tblBilletsJoin
Where (((tblBilletsJoin.ClearanceNum)=[forms]![frmClearance]![ClearanceNum])
AND ((tblBilletsJoin.BilletNum)=[forms]![frmClearance]![cboBillet]));"

But I don't remember what the command is to run that sql statement.  Do I
have to open a new recordsource?  And then is it just me.recordsource.add
("sql statement")?

I might be totally confusing myself.  I don't know.
mscertified - 30 Jan 2008 21:34 GMT
CurrentProject.Connection.Execute <sql statement>

-Dorian

> I have a many to many relationship between security clearances and billets.  
> a clearance can be associated with more than one billet and a single billet
[quoted text clipped - 22 lines]
>
> I might be totally confusing myself.  I don't know.
Jonathan Brown - 30 Jan 2008 21:52 GMT
Here's my code.  Now, nothing is happening at all.  I've checked the table
and there's no new records.  Did I use the insert statement correctly?

____________________________________________________________

On Error GoTo Err_cmdAdd_Click

If IsNull(Me.cboBillets) Then
   Exit Sub
Else

CurrentProject.Connection.Execute ("INSERT INTO tblBilletsJoin
(ClearanceNum,BilletNum) " & _
"SELECT tblBilletsJoin.ClearanceNum, tblBilletsJoin.BilletNum FROM
tblBilletsJoin " & _
"WHERE tblBilletsJoin.ClearanceNum = " & Me.ClearanceNum & " AND
tblBilletsJoin.BilletNum = " & Me.cboBillets & ";")

Me.lstBillet.Requery

End If

Exit_cmdAdd_Click:
   Exit Sub

Err_cmdAdd_Click:
   MsgBox Err.Description
   Resume Exit_cmdAdd_Click
__________________________________________________________________

Do I need to include the semicolon at the end of my sql statement?  I don't
remember if I have to if I'm doing it through code.

> CurrentProject.Connection.Execute <sql statement>
>
[quoted text clipped - 26 lines]
> >
> > I might be totally confusing myself.  I don't know.
Jonathan Brown - 30 Jan 2008 22:01 GMT
I just changed the code to the following and it still isn't doing anything.  
It's not even giving me an error message.

On Error GoTo Err_cmdAdd_Click

If IsNull(Me.cboBillets) Then
   Exit Sub
Else

Dim strSQL As String

strSQL = "INSERT INTO tblBilletsJoin (EmpNum,BilletNum) " & _
"SELECT tblBilletsJoin.EmpNum, tblBilletsJoin.BilletNum FROM tblBilletsJoin
" & _
"WHERE tblBilletsJoin.EmpNum = " & Me.EmpNum & " AND
tblBilletsJoin.BilletNum = " & Me.cboBillets & ";"

CurrentProject.Connection.Execute strSQL

Me.lstBillet.Requery

End If

Exit_cmdAdd_Click:
   Exit Sub

Err_cmdAdd_Click:
   MsgBox Err.Description
   Resume Exit_cmdAdd_Click

> Here's my code.  Now, nothing is happening at all.  I've checked the table
> and there's no new records.  Did I use the insert statement correctly?
[quoted text clipped - 59 lines]
> > >
> > > I might be totally confusing myself.  I don't know.
Jonathan Brown - 30 Jan 2008 22:22 GMT
> I just changed the code to the following and it still isn't doing anything.  
> It's not even giving me an error message.
[quoted text clipped - 89 lines]
> > > >
> > > > I might be totally confusing myself.  I don't know.
mscertified - 31 Jan 2008 15:36 GMT
The code looks ok as far as I can tell.
Use standard debugging techniques...
Display the SQL statement before it executes.
Try running the SQL standalone as a query.
Try using DEBUG to step thru the code to see what is happening.
You will learn far more by debugging this yourself that by getting a canned
answer.

-Dorian

> I just changed the code to the following and it still isn't doing anything.  
> It's not even giving me an error message.
[quoted text clipped - 89 lines]
> > > >
> > > > I might be totally confusing myself.  I don't know.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.