get a record and update that record

Posted on 2013-02-04
Last Modified: 2013-02-05
I am having a serious mental block.  I need to retrieve a record from my DB based on a query

dim rs as adodb.recordset
dim sSQL as string

set rs = new.adodb.recorset
sSQL = "Select balance from ctbal" & " where ctbalID = " & Me.txtID

when I debug print this I get:

Select balance from ctbal where ctbalID = 1

if the field in question "Balance" has a value say 2 then why does my code say "empty" when I place my cursor over the field.

Here is exactly what I am trying do accomplish:

1) Extract the field value of "balance" from my table CTbal.
2) Add to the field from my form to create a "new" balance
 3) Update my table to reflect the "new" balance
Question by:sptech
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4

Expert Comment

ID: 38850821
What is the datatype of the ctbalID column? If it is not numeric then your query should look like this:

sSQL = "Select balance from ctbal " & " where ctbalID = '" & Me.txtID & "'"

Select balance from ctbal where ctbalID = '1'
LVL 14

Expert Comment

by:Bill Ross
ID: 38850844

Seems like one step to me.

Sim strSQL as string
strSql = "UPDATE ctbal SET balance = (balance + " & me!.fieldfrommyform & ") where ctbalID = '" & Me!txtID & "'"
currentdb.execute strSql

No need to create a recordset - just do the update.



Author Comment

ID: 38850909
the field inside the table "balance is a HH:NN (short time) field as is the field on my form.  The balance field inside my table is 01:00.  The me.fieldname is 10:25 (also short time)

when I try the Balance+me.fieldname I get (10:2501:00)
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

LVL 14

Expert Comment

by:Bill Ross
ID: 38851012

If you are trying to add time then you must add correct delimiter.  Try this:

strSql = "UPDATE ctbal SET balance = (balance + #" & me!fieldname & "#) where ctbalID = '" & Me!txtID & "'"
Date/time requires #



Author Comment

ID: 38851120
  I got the time issue resolved (it isn't as clean as yours but it works)(I will try your solution in the morning).  

I am getting an error when I try to execute the strsql string you created for me.  I think it has to do with currentdb.execute, strsql.  What should I have done initially to get the db ready to update?
LVL 14

Expert Comment

by:Bill Ross
ID: 38851690

What is the error message?

What is the table structure of ctbal?

currentdb.execute does not need any prep - it just runs.



Author Comment

ID: 38853794
I am getting "argument not optional"  the field is a date/time field with short time. when I hover over the me.fieldname instead of seeing 10:25 (short time) I see 10:25:00 am. I am using ADO and not DAO can this be part of the issue?
I have the sub listed below:

Private Sub CTBal()
Dim sSQL As String

On Error GoTo err_label

sSQL = "update ctbal set balance = (#" & balance & "# + #" & Me!txtTCTAppr & "# ) where ID =  ' " & Me!txtID & " ' "
Debug.Print sSQL
CurrentDb.Execute , sSQL

Exit Sub

    MsgBox "Error creating new record" & vbNewLine & Err.Description, vbCritical, "Error creating new record"

End Sub

the results of the debug.print:

update ctbal set balance = (#1:00:00 AM# + #10:25:00 AM# ) where ID = '1'

Author Comment

ID: 38854378
  A little clarification on what I am trying to accomplish.

My users travel while traveling they earn comp time vice over time.  I am trying to keep track of how much they have earned, used and the balance.  I can track how much earned, how much used, but the balance issue is kicking my behind.  In my earlier comment I have an existing balance of 1 hour, my user earned 10 hours 25 minutes for a grand total of 11 and 25 minutes.  "Balance" shows when I hover the cursor to be 11 hours and 25 minutes, but the currentdb.execute, ssql is throwing the error "argument not optional"

to add a monkey wrench I will have to eventually add logic to round to the nearest quarter hour.
LVL 14

Accepted Solution

Bill Ross earned 500 total points
ID: 38855511

I don't think it's a good idea to keep results of calculations in a table.  If you know how much they have earned (tblEarnedTime) and how much they have used (tblTimeUsed) ou can always get the balance.  No need to try to update the balance as you go.  Look at your checkbook as an example.

A query is an easy tool to use for the answer and subtract the total of one from another.

The expression below will give you decimal hours rounded to the nearest .25 hours between 2 time fields (me!fldStop and me!fldStart) and set the fldNearestQtrHour field.

me!fldNearestQtrHour=round(((me!fldStop-me!fldStart1) * 60 * 24) / 60 /.25,0) * .25

Put this code in the after update event of your form to save the hours earned and hours used.  Use a query anytime the balance is needed.



Author Closing Comment

ID: 38858201
Bill gave me answers to two issues in this question. I greatly appreciate his helping a newbie such as myself.

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question