Command button to input data in a table (1 Viewer)

wilsgaard

New member
Local time
Today, 20:17
Joined
Aug 8, 2011
Messages
3
I'm a rookie in access programming, so I need some basic help.

I need a command button to input data into certain fields in a table.

Example:

I have a table (Table1) with four fields: Field1, Field2, Field3 and Field4.

A form (Form1) is linked to Table1. In the form I have two buttons: Button1 and Button2.

When I click Button1, I want it to input the following data in the last record in Table1:

Field1 = 1, Field2 = 0, Field3 = 0, Field4 = 0.

When I click Button2, I want it to input the following data in the last record in Table1:

Field1 = 0, Field2 = 1, Field3 = 0, Field4 = 0.

Can anyone help with this?
 

John Big Booty

AWF VIP
Local time
Tomorrow, 06:17
Joined
Aug 29, 2005
Messages
8,263
Welcome to the forum.

In the On Click event of your buttons' you could use some code like;
Code:
    If Me.NewRecord = False Then
        DoCmd.GoToRecord acDataForm, "YourFormName", acNewRec
        Me.Field1 = 1
        Me.Field2 = 0
        Me.Field3 = 0
        Me.Field4 = 0
    End If
You will need to change the code to reflect what you want to achieve for each button.
 

wilsgaard

New member
Local time
Today, 20:17
Joined
Aug 8, 2011
Messages
3
Am I totaly wrong if I write it like this?:


Private Sub Kommando21_Click()
If Me.NewRecord = False Then
DoCmd.GoToRecord acDataForm, "Table1", acNewRec
Me.Field1 = 1
Me.Field2 = 0
Me.Field3 = 0
Me.Field4 = 0
End If
End Sub



I get this message when I click it:
"Compile error:

Invalid outside procedure"



I have also tried a code like this, but then it always creates a new record:


Option Compare Database

Private Sub Kommando165_Click()


DoCmd.SetWarnings False


DoCmd.RunSQL "INSERT INTO [Sjanseanalyse] ([Field1], [Field2], [Field3], [Field4]) VALUES (1, 0, 0, 0);"

DoCmd.SetWarnings True

End Sub
 

John Big Booty

AWF VIP
Local time
Tomorrow, 06:17
Joined
Aug 29, 2005
Messages
8,263
Sorry my bad :eek:

The out put should be enclosed in double quotes and look like;
Code:
    If Me.NewRecord = False Then
        DoCmd.GoToRecord acDataForm, "YourFormName", acNewRec
        Me.Field1 = "1"
        Me.Field2 = "0"
        Me.Field3 = "0"
        Me.Field4 = "0"
    End If

The above code will check if the current record is new and if not take you to a new record. If you simply want to set the values of fields 1-4 to the desired values simply use;
Code:
        Me.Field1 = "1"
        Me.Field2 = "0"
        Me.Field3 = "0"
        Me.Field4 = "0"
 

wilsgaard

New member
Local time
Today, 20:17
Joined
Aug 8, 2011
Messages
3
Nothing happens when I click the button.

This line is marked yellow in the code:

DoCmd.GoToRecord acDataForm, "Table2", acNewRec
 

John Big Booty

AWF VIP
Local time
Tomorrow, 06:17
Joined
Aug 29, 2005
Messages
8,263
Firstly what is the error message you are getting?

It is likely due to the fact that the code;
Code:
DoCmd.GoToRecord [B][COLOR="Magenta"]acDataForm[/COLOR][/B], "Table2", acNewRec
has specified that the object being refereed to should be a form, however you have specified a table called Table2 :confused:
 

JeanieGreene

Registered User.
Local time
Today, 15:17
Joined
Nov 13, 2017
Messages
24
Sorry to drag up an old post, but I believe this programming is exactly what I am looking for. But, I am getting an error.

I have a basic countdown timer that has a text box that I can add a persons name/radio call sign in. We use this timer to perform "status checks" on law enforcement officers when they are on a stop. I want a command button that when clicked takes the information in the text box and fills in a new record on a form, and restarts the timer.

This is the code I am using...

TimerInterval = 0
TimeCount = 300
Loops = 0
Me.txtCounter.BackColor = vbWhite

If Me.NewRecord = False Then
DoCmd.GoToRecord acDataForm, Main_Page, acNewRec
Me.FunctionKey = "i"
Me.UnitCalling = "[&Text16&]"
Me.Notes = "[code 4 still out]"
End If

The form I want to enter data into is Main_Page, my fields on the form are FunctionKey, UnitCalling and Notes. Everything is spelled the same in the code as well as the form/field names. But, I am still getting an error code "Compile error: Method or data member not found" with Me.FunctionKey highlighted. :banghead:

Thank you in advance!
 

isladogs

MVP / VIP
Local time
Today, 19:17
Joined
Jan 14, 2017
Messages
18,186
Sorry but there's a few things I don't understand here:

Code:
TimerInterval = 0
TimeCount = 300
Loops = 0

...
Me.FunctionKey = "i"

1. If the timer interval = 0, any timer code is disabled
2. What does TimeCount = 300 mean? Wait 5 minutes? But the timer is disabled
3. Loops = 0 - there aren't any shown in your code
4. Function key = "i" - please explain ...

I'm also not sure how this relates to the old post

Apart from that, its all perfectly clear ! :D

Presumably you have a control called Text16 on the form
 

JeanieGreene

Registered User.
Local time
Today, 15:17
Joined
Nov 13, 2017
Messages
24
Thank you, the timer interval is supposed to be 1000. I have corrected that. The Loops=0 may be unnecessary. I was having issues before with the timer running on open, even though I had a "on click" command. I figured that out, so I might be able to delete that.

The Me.FunctionKey is for a field in my form named FunctionKey. I want it to populate as i (dispatch code for something the dispatcher did).

Thanks for looking at it.

Is there any other reason I could be getting this error code? I am still getting the error code with the timer interval and loops changes.
 
Last edited:

JeanieGreene

Registered User.
Local time
Today, 15:17
Joined
Nov 13, 2017
Messages
24
I am still getting the error that Me.FunctionKey cannot be found. I have even copied the name of the field and pasted it in the code to make sure I am getting it correct. Any reason why I am getting this error?(Method or data member not found)

Thank you for nay help you can provide. I am ready to pull my hair out!
 

JeanieGreene

Registered User.
Local time
Today, 15:17
Joined
Nov 13, 2017
Messages
24
It is not recognizing any of my fields on the Main_Page form. This form does have subforms attached. Will that make a difference?

Also, I have removed the Labels above each field, but the fields are still named appropriately. So, I don't think this is the problem, but I am putting it out there for those who are way smarter then I am. :)
 

Mark_

Longboard on the internet
Local time
Today, 12:17
Joined
Sep 12, 2017
Messages
2,111
Is this code being executed by something on "Main_Page" or from another form?
 

isladogs

MVP / VIP
Local time
Today, 19:17
Joined
Jan 14, 2017
Messages
18,186
You need to give us more to go on in order to assist you properly
Perhaps post your database or at the very least the full code relevant to this topic.

And can I ask again, what exactly does this have to do with the rest of this thread as I see no link at all from what you've posted.
 

JeanieGreene

Registered User.
Local time
Today, 15:17
Joined
Nov 13, 2017
Messages
24
Mark, This is code on form Timer1. When I push the button on timer 1 I want it to enter data into the Radiolog table through the Main_Page form. Maybe I should skip going through the form and enter it directly into the table. Hmm.

Let me figure out how to post the database. I am sure that will be helpful.

While I do that, this is the code...

Private Sub StatusCheck_Click()
TimerInterval = 1000
TimeCount = 300

Me.txtCounter.BackColor = vbWhite

If Me.NewRecord = False Then
DoCmd.GoToRecord acDataForm, Main_Page, acNewRec
Me.FunctionKey = "i"
Me.UnitCalling = "[&Text16&]"
Me.Notes = "[code 4 still out]"
End If

End Sub
 

JeanieGreene

Registered User.
Local time
Today, 15:17
Joined
Nov 13, 2017
Messages
24
Here is my zipped database...I hope.:)
 

Attachments

  • GreeneCAD.zip
    210 KB · Views: 96

Mark_

Longboard on the internet
Local time
Today, 12:17
Joined
Sep 12, 2017
Messages
2,111
When you preface a field with "Me.", that means you are referring to a control on the same form that your code is executing on.

You would need to reference FunctionKey as

Forms!Main_Page!FunctionKey

For more about how to reference controls on other forms read HERE
 

JeanieGreene

Registered User.
Local time
Today, 15:17
Joined
Nov 13, 2017
Messages
24
That makes sense. Thank you. Do I need to change my DoCmd statement? I am thinking that I will need to put an additional DoCmd statement before my If Then statement to Open the form?
 
Last edited:

JeanieGreene

Registered User.
Local time
Today, 15:17
Joined
Nov 13, 2017
Messages
24
It all works now. Thank you! In case anyone else could use the help, here is what I ended up using for the code...

Private Sub StatusCheck_Click()
TimerInterval = 1000
TimeCount = 300

Me.txtCounter.BackColor = vbWhite
DoCmd.OpenForm "Main_Page", , , , acFormAdd
Forms!Main_Page!FunctionKey = "i"
Forms!Main_Page!UnitCalling = Me.Text16
Forms!Main_Page!Notes = "code 4 still out"
Forms!Main_Page.Requery
Forms!Main_Page!FunctionKey.SetFocus
End Sub
 

Users who are viewing this thread

Top Bottom