Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    May 2002
    Posts
    11

    Unanswered: append data from a textbox in a form to a table

    HALLO
    is it possible to append data from a text box in a form to a table.
    for example:

    i have 'form1' and within it i have 'text1' i also have 'table1'
    i want to type something into 'text1' and when i press 'command1' i want it to append text1.text to 'table1'


    can nebody hlp me plz
    cheers

  2. #2
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    117
    Sure,

    Something like this,

    Dim db As Database
    Dim rs As DAO.Recordset
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Table1")
    With rs
    .AddNew
    .Fields("Field1") = Me.FormField
    .Update
    .Close
    End With

    This procedure will add a new record into table1 with the data from your form in FormField. Note: You can use as many fields as your table has, just add them:
    .Fields("Field2")
    .Fields("Field3")
    etc...

    Does this help??
    Kal

  3. #3
    Join Date
    May 2002
    Posts
    11
    i think i get it, but where would i put this, as i want it to happen on a command_click and record to add will be in a textbox

  4. #4
    Join Date
    May 2002
    Posts
    11
    ok i thnk i figerr'd it out:

    Dim db As Database
    Dim rs As DAO.Recordset

    Sub lblTownAdd_Click()
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("tblTOWNLIST")
    With rs
    .AddNew
    .Fields("TOWN NAME") = Me.txtTown
    .Update
    .Close
    End With
    End Sub

    but wen i click lblTownAdd i get told:

    "the expression on click you entered as the event property setting produced the following error:user-defined type not defined"

  5. #5
    Join Date
    May 2002
    Posts
    11
    ok...dont worry i have managed to sort it out.

    but how do i get it to check whether the entry exists already or not.
    I.E. on click it will check thetable to see if the record is there, if it is it will return an error, if it isnt it will give a msgbox saying "txtTown has beeen added"

  6. #6
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    117
    Hey,

    Try this,

    Sub lblTownAdd_Click()
    Dim db As Database
    Dim rs As DAO.Recordset
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT * FROM tblTownList WHERE([Town Name] = """ & me.txtTown & """")")
    If rs.EOF Then
    With rs
    .AddNew
    .Fields("TOWN NAME") = Me.txtTown
    .Update
    .Close
    End With
    Else
    MsgBox "Entry already exists."
    End If
    End Sub

    Also, may I make a suggestion. You really need to make sure that all of your field names do not have spaces in them. Do something like Town_Name or TownName. It will really make programming easier.

    Later Kal

  7. #7
    Join Date
    May 2002
    Posts
    11
    rite... i put wot u sed n got an error...so i chngd ur code 2:

    Sub lblTownAdd_Click()
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT * FROM tblTownList WHERE([Town Name] = "" & me.txtTown & "")")
    If rs.EOF Then
    With rs
    .AddNew
    .Fields("TOWN NAME") = txtTown.Text
    .Update
    .Close
    End With
    Else
    MsgBox "Entry already exists."
    End If
    End Sub

    it adds th record fine, but if it exists it dusnt giv an error, it just adds it

  8. #8
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    117
    Try and change the code to this,

    Sub lblTownAdd_Click()
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT * FROM tblTownList WHERE([Town Name] = """ & me.txtTown & """)")
    If rs.EOF Then
    With rs
    .AddNew
    .Fields("TOWN NAME") = txtTown.Text
    .Update
    .Close
    End With
    Else
    MsgBox "Entry already exists."
    End If
    End Sub

    I believe there needs to be three(3) parenthesis on either side of & Me.txtTown &. 'K'

  9. #9
    Join Date
    May 2002
    Posts
    11
    tried it...same problem, it just adds even if the entry exists

  10. #10
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    117
    HMMMMM......

    I you sure that your checking the right table and field for the town name? Are the tables and fields spelled properly? Have you tried taking the spaces out of your field names. There has to be something were over looking here.

  11. #11
    Join Date
    May 2002
    Posts
    11
    chkd it all....
    chngd th field name 2 TOWN_NAME
    even chngd th case 2 match
    same prblm.

    im no Xpr but is ther a replacemnt 4 EOF
    cos it mite b that?

  12. #12
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    117
    post a copy of the database in a zip format so I can look at it.

  13. #13
    Join Date
    May 2002
    Posts
    11
    i cut most of th thngs out 2 make it small
    Attached Files Attached Files

  14. #14
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    117
    Hey,

    Your problem lies with your click label. Your using a label instead of a button. Whats happening is when you click on your label the focus has not left the field txtTown and therefore access thinks the field is blank. All fields are left in a Zero Length or Null state until the field has lost the focus. You need to do one of two things, add a line like me.txtCounty.setFocus or change your label to a click button so that the button can take the focus.

    Just so you understand, try this:

    Type a Town name in your field and then before clicking your add label place your cursor in the county field, (hense the town field has lost the focus) and then click on your add label twice. You will now get a message box.

    Make sure you write back so I know that you understand. 'k'

  15. #15
    Join Date
    May 2002
    Posts
    11
    rite...i clikd on th county box n typd n entree that xists...th msgbox coms up all is good.

    thn i typd summin that dusnt Xist...pressd add...n i get an error...
    it ses that it needs to have the focus

    the problem lies within th line:
    .Fields("TOWN_NAME") = txtTown.Text

    as it is highlighted wen i press debug

    ne help?
    Last edited by homicidal10denC; 05-21-02 at 16:43.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •