{"id":137,"date":"2004-06-18T21:54:58","date_gmt":"2004-06-18T19:54:58","guid":{"rendered":"http:\/\/www.marblestation.com\/blog\/?p=137"},"modified":"2012-04-26T19:09:12","modified_gmt":"2012-04-26T17:09:12","slug":"visual-basic-for-applications-ms-access","status":"publish","type":"post","link":"https:\/\/www.marblestation.com\/?p=137","title":{"rendered":"Visual Basic for Applications (MS Access)"},"content":{"rendered":"<p>Entre ayer y hoy he aprendido a utilizar VBA en Access para realizar unas modificaciones en unas bases de datos, lo pongo aqui como referencia por si vuelvo a necesitar hacer algo del estilo:<\/p>\n<pre>\r\n' ExecuteQuery: Realitza una modificaci\u00f3 a la BD\r\n' Provar amb:\r\n'  ExecuteQuery \"INSERT INTO CamaraEnvasadoPreenvasado VALUES ('test', 'borrar')\"\r\nSub ExecuteQuery(strSQL As String)\r\n   Dim cnn          As ADODB.Connection\r\n   Dim lngAffected  As Long\r\n   \r\n   ' Open the connection.\r\n   Set cnn = CurrentProject.Connection\r\n\r\n   ' Execute the query.\r\n   cnn.Execute CommandText:=strSQL, _\r\n               RecordsAffected:=lngAffected, _\r\n               Options:=adExecuteNoRecords\r\n\r\n   'Debug.Print \"Records Affected = \" & lngAffected\r\n   \r\n   ' Close connection and destroy object variables.\r\n   cnn.Close\r\n   Set cnn = Nothing\r\nEnd Sub\r\n\r\n' TransformDB: Agafa els registres de Frustas_Hortalizas amb el\r\n' camp \"checkField\" a true i guarda el num. suscriptor junt amb\r\n' \"idEnvase\" a la taula RelacionEnvase\r\n' Provar amb: TransformDB \"Pre_codigo_PLU\", 25\r\nSub TransformDB(checkField As String, idEnvase As Integer)\r\n    Dim cnnDB As ADODB.Connection\r\n    Dim recordSt As New ADODB.Recordset\r\n    Dim strSQL As String\r\n    Dim idSuscriptor As String\r\n    Dim i As Integer\r\n   \r\n    ' Connectar a la BD actual:\r\n    Set cnnDB = CurrentProject.Connection\r\n\r\n    ' Obtenim els registres que tenen el camp indicat a true\r\n    strSQL = \"SELECT * FROM Frustas_Hortalizas WHERE \" & checkField & \" = -1\"\r\n    With recordSt\r\n        Set .ActiveConnection = cnnDB\r\n        .CursorType = adOpenKeyset\r\n        .CursorLocation = adUseClient\r\n        .LockType = adLockOptimistic\r\n        .Open strSQL\r\n    End With\r\n    \r\n    ' Insertar a la taula RelacionEnvases una relacio entre\r\n    ' l'idSusbcriptor dels registres trobats i el idEnvas corresponent\r\n    ' al camp que hem comprovat\r\n    i = 0\r\n    If Not recordSt.EOF Then\r\n        recordSt.MoveFirst\r\n    End If\r\n    Do While Not recordSt.EOF\r\n        idSuscriptor = recordSt.Fields(\"Numero de suscriptor\").Value\r\n        ' Insertar amb ID de \"envasado\" 26\r\n        ExecuteQuery \"INSERT INTO RelacionEnvases VALUES ('\" & idSuscriptor & \"','\" & idEnvase & \"')\"\r\n        i = i + 1\r\n        recordSt.MoveNext\r\n    Loop\r\n    'Debug.Print \"Fields \" & recordSt.Fields.Count\r\n    Debug.Print \"Found \" & i & \" registers with field \" & checkField & \" = true. idEnvase = \" & idEnvase\r\n   ' Close Connection object and destroy object variable.\r\n   cnnDB.Close\r\n   Set cnnDB = Nothing\r\nEnd Sub\r\n<\/pre>\n<p>Mientras editamos el modulo en MS Access podemos pulsar CTRL+G para mostrar la ventana &#8220;Inmediato&#8221; y ahi se pueden ejecutar las funciones.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Entre ayer y hoy he aprendido a utilizar VBA en Access para realizar unas modificaciones en unas bases de datos, lo pongo aqui como referencia por si vuelvo a necesitar hacer algo del estilo: &#8216; ExecuteQuery: Realitza una modificaci\u00f3 a la BD &#8216; Provar amb: &#8216; ExecuteQuery &#8220;INSERT INTO CamaraEnvasadoPreenvasado VALUES (&#8216;test&#8217;, &#8216;borrar&#8217;)&#8221; Sub ExecuteQuery(strSQL &hellip; <a href=\"https:\/\/www.marblestation.com\/?p=137\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic for Applications (MS Access)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,6],"tags":[],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-espanyol","category-tecnologia"],"_links":{"self":[{"href":"https:\/\/www.marblestation.com\/index.php?rest_route=\/wp\/v2\/posts\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marblestation.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marblestation.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marblestation.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marblestation.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=137"}],"version-history":[{"count":1,"href":"https:\/\/www.marblestation.com\/index.php?rest_route=\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":1787,"href":"https:\/\/www.marblestation.com\/index.php?rest_route=\/wp\/v2\/posts\/137\/revisions\/1787"}],"wp:attachment":[{"href":"https:\/\/www.marblestation.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marblestation.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marblestation.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}